CodeCraft-20 (Div. 2) D. Nash Matrix

题目链接:http://codeforces.com/contest/1316/problem/D

题意:你有一个n*n的棋盘,每个格子都有一个操作:往上走U,往下走D,往左走L,往右走R,呆在原地X。现在给出从每个格子开始走最后会停在的地方(x,y),如果最后不会停下,则x=-1&&y=-1;然后让你输出每个格子是哪种操作,如果不存在就输出INVALID。

思路:首先对于不会停下的点单独一个在一起,那肯定是不存在的,然后我们可以找一条长度大于等于2的一条路线,A-> B-> C <- D  。那条路线像这样一样就形成了回路,然后其他相连也是不会停下的点就汇入这条路线中。对于最终会在某个地方停下的点,如果从该点走不到(只能走最终点一样的点)最终点,那就是不存在,否则还是找到一条从该点走到目标点的路线,然后其他最终点一样的点就汇入这条路线就可以了。我都是用bfs去敲的,代码有些复杂。

  1 #include<cstdio>
  2 #include<cstring>
  3 #include<cmath>
  4 #include<string>
  5 #include<iostream>
  6 #include<algorithm>
  7 #include<map>
  8 #include<vector>
  9 #include<queue>
 10 using namespace std;
 11 struct node
 12 {
 13     int x,y;
 14 } a[1005][1005];
 15 int book[1005][1005];
 16 int b[1005][1005];
 17 int Next[4][2]= {{1,0},{-1,0},{0,1},{0,-1}};
 18 struct no
 19 {
 20     int x,y;
 21     int z,w,s;
 22 } que[1000006];
 23 void bfs(int x,int y)
 24 {
 25     int head=0,tail=1;
 26     que[0].x=x;
 27     que[0].y=y;
 28     que[0].z=1;
 29     book[x][y]=1;
 30     int k=0,w;
 31     while(head<tail)
 32     {
 33         for(int i=0; i<4; i++)
 34         {
 35             int tx=que[head].x+Next[i][0];
 36             int ty=que[head].y+Next[i][1];
 37             if(a[tx][ty].x!=-1||book[tx][ty]==1)
 38                 continue;
 39             que[tail].x=tx;
 40             que[tail].y=ty;
 41             book[tx][ty]=1;
 42             if(que[head].z==1)
 43             {
 44                 que[tail].z=1;
 45                 k=tail;
 46                 b[que[head].x][que[head].y]=i;
 47                 que[head].z=0;
 48                 if(i==0)
 49                     w=1;
 50                 else if(i==1)
 51                     w=0;
 52                 else if(i==2)
 53                     w=3;
 54                 else if(i==3)
 55                     w=2;
 56             }
 57             else
 58             {
 59                 que[tail].z=0;
 60                 if(i==0)
 61                     b[tx][ty]=1;
 62                 else if(i==1)
 63                     b[tx][ty]=0;
 64                 else if(i==2)
 65                     b[tx][ty]=3;
 66                 else if(i==3)
 67                     b[tx][ty]=2;
 68             }
 69             tail++;
 70         }
 71         head++;
 72     }
 73     int tx=que[k].x;
 74     int ty=que[k].y;
 75     b[tx][ty]=w;
 76 }
 77 int dfs(int x,int y)
 78 {
 79     int head=0,tail=1;
 80     que[0].x=x;
 81     que[0].y=y;
 82     que[0].w=0;
 83     que[0].s=-1;
 84     book[x][y]=1;
 85     int v=0,k,t;
 86     while(head<tail)
 87     {
 88         for(int i=0; i<4; i++)
 89         {
 90             int tx=que[head].x+Next[i][0];
 91             int ty=que[head].y+Next[i][1];
 92             if(tx==a[x][y].x&&ty==a[x][y].y&&v==0)
 93             {
 94                 v=1;
 95                 k=head;
 96                 t=i;
 97                 continue;
 98             }
 99             if(book[tx][ty]==1||a[tx][ty].x!=a[x][y].x||a[tx][ty].y!=a[x][y].y)
100                 continue;
101             book[tx][ty]=1;
102             que[tail].z=i;
103             que[tail].x=tx;
104             que[tail].y=ty;
105             que[tail].w=0;
106             que[tail].s=head;
107             tail++;
108         }
109         head++;
110     }
111     if(v==0)
112         return 0;
113     int xx=que[k].z;
114     que[k].w=1;
115     b[que[k].x][que[k].y]=t;
116     while(1)
117     {
118         k=que[k].s;
119         if(k==-1)
120             break;
121         int tx=que[k].x;
122         int ty=que[k].y;
123         b[tx][ty]=xx;
124         que[k].w=1;
125         xx=que[k].z;
126     }
127     for(int i=0; i<head; i++)
128     {
129         if(que[i].w==1)
130             continue;
131         int tx=que[i].x;
132         int ty=que[i].y;
133         if(que[i].z==0)
134             b[tx][ty]=1;
135         else if(que[i].z==1)
136             b[tx][ty]=0;
137         else if(que[i].z==2)
138             b[tx][ty]=3;
139         else if(que[i].z==3)
140             b[tx][ty]=2;
141     }
142     return 1;
143 }
144 int main()
145 {
146     int n;
147     scanf("%d",&n);
148     for(int i=1; i<=n; i++)
149     {
150         for(int j=1; j<=n; j++)
151         {
152             scanf("%d%d",&a[i][j].x,&a[i][j].y);
153             if(a[i][j].x==i&&a[i][j].y==j)
154             {
155                 b[i][j]=4;
156                 book[i][j]=1;
157             }
158         }
159     }
160     for(int i=1; i<=n; i++)
161         for(int j=1; j<=n; j++)
162         {
163             if(book[i][j]==1)
164                 continue;
165             if(a[i][j].x==-1)
166             {
167                 if(a[i][j-1].x!=-1&&a[i][j+1].x!=-1&&a[i-1][j].x!=-1&&a[i+1][j].x!=-1)
168                 {
169                     printf("INVALID\n");
170                     return 0;
171                 }
172                 bfs(i,j);
173             }
174             else
175             {
176                 int tx=a[i][j].x;
177                 int ty=a[i][j].y;
178                 if(tx!=a[tx][ty].x||ty!=a[tx][ty].y)
179                 {
180                     printf("INVALID\n");
181                     return 0;
182                 }
183                 int xx=dfs(i,j);
184                 if(xx==0)
185                 {
186                     printf("INVALID\n");
187                     return 0;
188                 }
189             }
190         }
191     printf("VALID\n");
192     for(int i=1; i<=n; i++)
193     {
194         for(int j=1; j<=n; j++)
195         {
196             if(b[i][j]==0)
197                 printf("D");
198             else if(b[i][j]==1)
199                 printf("U");
200             else if(b[i][j]==2)
201                 printf("R");
202             else if(b[i][j]==3)
203                 printf("L");
204             else if(b[i][j]==4)
205                 printf("X");
206         }
207         printf("\n");
208     }
209 }

 

posted @ 2020-03-05 11:27  ~zcb  阅读(172)  评论(0编辑  收藏  举报