#include <stdio.h> #include <vector> #include <algorithm> typedef long long int llint; int n,m,k; llint nm; int**t; bool*poss; int*u; int*v; /* 3 5 7 0 1 123 1 0 0 4 8 0 2 2 16 2 3 0 18 19 17 3 0 0 */ void dump() { printf("GRAPH:\n"); for(int i=0;i<n;++i) { for(int j=0;j<m;++j) printf("%2d ", t[j][i]); printf("\n"); } printf("PATH:"); for(int i=0;i<n+m-1;++i) printf("%d %d %c |",u[i],v[i],poss[i]?'Y':'N'); printf("\n"); } int base_p; bool find_path_rec(int p,int a,int b,bool first=false) { //printf("PRE-VISIT p=%d %d %d\n",p,a,b); if(a==m)return false; if(b==n)return false; if(t[a][b]==-2)return false; //printf("VISIT %d %d\n",a,b); if(!first&&(t[a][b]==p&&a==u[p]&&b==v[p])) return p>base_p; int nexta=a+1; int nextb=b; if(!find_path_rec(p+1,nexta,nextb)) { nexta=a; nextb=b+1; if(!find_path_rec(p+1,nexta,nextb)) { if(!(t[a][b]==p&&a==u[p]&&b==v[p])) t[a][b]=-2; return false; } } t[a][b]=p; u[p]=a; v[p]=b; poss[p]=true; return true; } bool find_path(int p) { //printf("FIND p=%d\n",p); return find_path_rec(p,u[p],v[p],true); } bool put_base(int a,int b) { int p=t[a][b]; base_p=p; if(p<0||a!=u[p]||b!=v[p]) { //printf("JUST PUT\n"); t[a][b]=-2; return true; } t[a][b]=-2; while(p>0&&poss[p-1]) { if(find_path(p-1)) return true; poss[p-1]=false; --p; } t[a][b]=base_p; return false; } int main() { scanf("%d %d %d",&n,&m,&k); nm=n*m; t=new int*[m]; for(int i=0;i<m;++i) { t[i]=new int[n]; for(int j=0;j<n;++j) t[i][j]=-1; } poss=new bool[n+m-1]; u=new int[n+m-1]; v=new int[n+m-1]; for(int i=0;i<n+m-1;++i)poss[i]=true; for(int i=0;i<m;++i) { t[i][0]=i; u[i]=i; v[i]=0; } for(int i=1;i<n;++i) { t[m-1][i]=m+i-1; u[m+i-1]=m-1; v[m+i-1]=i; } //dump(); //return 0; int x=0; for(int i=0;i<k;++i) { int r,c,z; scanf("%d %d %d",&r,&c,&z); int a=(c^x)%m; int b=(r^x)%n; //printf("PUT %d %d\n",a,b); if(put_base(a,b)) printf("NIE\n"); else { x=x^z; printf("TAK\n"); } //dump(); //if(i==3)return 0; } return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | #include <stdio.h> #include <vector> #include <algorithm> typedef long long int llint; int n,m,k; llint nm; int**t; bool*poss; int*u; int*v; /* 3 5 7 0 1 123 1 0 0 4 8 0 2 2 16 2 3 0 18 19 17 3 0 0 */ void dump() { printf("GRAPH:\n"); for(int i=0;i<n;++i) { for(int j=0;j<m;++j) printf("%2d ", t[j][i]); printf("\n"); } printf("PATH:"); for(int i=0;i<n+m-1;++i) printf("%d %d %c |",u[i],v[i],poss[i]?'Y':'N'); printf("\n"); } int base_p; bool find_path_rec(int p,int a,int b,bool first=false) { //printf("PRE-VISIT p=%d %d %d\n",p,a,b); if(a==m)return false; if(b==n)return false; if(t[a][b]==-2)return false; //printf("VISIT %d %d\n",a,b); if(!first&&(t[a][b]==p&&a==u[p]&&b==v[p])) return p>base_p; int nexta=a+1; int nextb=b; if(!find_path_rec(p+1,nexta,nextb)) { nexta=a; nextb=b+1; if(!find_path_rec(p+1,nexta,nextb)) { if(!(t[a][b]==p&&a==u[p]&&b==v[p])) t[a][b]=-2; return false; } } t[a][b]=p; u[p]=a; v[p]=b; poss[p]=true; return true; } bool find_path(int p) { //printf("FIND p=%d\n",p); return find_path_rec(p,u[p],v[p],true); } bool put_base(int a,int b) { int p=t[a][b]; base_p=p; if(p<0||a!=u[p]||b!=v[p]) { //printf("JUST PUT\n"); t[a][b]=-2; return true; } t[a][b]=-2; while(p>0&&poss[p-1]) { if(find_path(p-1)) return true; poss[p-1]=false; --p; } t[a][b]=base_p; return false; } int main() { scanf("%d %d %d",&n,&m,&k); nm=n*m; t=new int*[m]; for(int i=0;i<m;++i) { t[i]=new int[n]; for(int j=0;j<n;++j) t[i][j]=-1; } poss=new bool[n+m-1]; u=new int[n+m-1]; v=new int[n+m-1]; for(int i=0;i<n+m-1;++i)poss[i]=true; for(int i=0;i<m;++i) { t[i][0]=i; u[i]=i; v[i]=0; } for(int i=1;i<n;++i) { t[m-1][i]=m+i-1; u[m+i-1]=m-1; v[m+i-1]=i; } //dump(); //return 0; int x=0; for(int i=0;i<k;++i) { int r,c,z; scanf("%d %d %d",&r,&c,&z); int a=(c^x)%m; int b=(r^x)%n; //printf("PUT %d %d\n",a,b); if(put_base(a,b)) printf("NIE\n"); else { x=x^z; printf("TAK\n"); } //dump(); //if(i==3)return 0; } return 0; } |