Nash Matrix CodeForces - 1316D(DFS)

Nash designed an interesting yet simple board game where a player is simply required to follow instructions written on the cell where the player currently stands.

This board game is played on the n×n board. Rows and columns of this board are numbered from 1 to n. The cell on the intersection of the r-th row and c-th column is denoted by (r,c).

Some cells on the board are called blocked zones. On each cell of the board, there is written one of the following 5 characters — U, D, L, R or X — instructions for the player. Suppose that the current cell is (r,c). If the character is R, the player should move to the right cell (r,c+1), for L the player should move to the left cell (r,c−1), for U the player should move to the top cell (r−1,c), for D the player should move to the bottom cell (r+1,c). Finally, if the character in the cell is X, then this cell is the blocked zone. The player should remain in this cell (the game for him isn’t very interesting from now on).

It is guaranteed that the characters are written in a way that the player will never have to step outside of the board, no matter at which cell he starts.

As a player starts from a cell, he moves according to the character in the current cell. The player keeps moving until he lands in a blocked zone. It is also possible that the player will keep moving infinitely long.

For every of the n2 cells of the board Alice, your friend, wants to know, how will the game go, if the player starts in this cell. For each starting cell of the board, she writes down the cell that the player stops at, or that the player never stops at all. She gives you the information she has written: for each cell (r,c) she wrote:

a pair (x,y), meaning if a player had started at (r,c), he would end up at cell (x,y).
or a pair (−1,−1), meaning if a player had started at (r,c), he would keep moving infinitely long and would never enter the blocked zone.
It might be possible that Alice is trying to fool you and there’s no possible grid that satisfies all the constraints Alice gave you. For the given information Alice provided you, you are required to decipher a possible board, or to determine that such a board doesn’t exist. If there exist several different boards that satisfy the provided information, you can find any of them.

Input
The first line of the input contains a single integer n (1≤n≤103) — the side of the board.

The i-th of the next n lines of the input contains 2n integers x1,y1,x2,y2,…,xn,yn, where (xj,yj) (1≤xj≤n,1≤yj≤n, or (xj,yj)=(−1,−1)) is the pair written by Alice for the cell (i,j).

Output
If there doesn’t exist a board satisfying the information that Alice gave you, print a single line containing INVALID.

Otherwise, in the first line print VALID. In the i-th of the next n lines, print the string of n characters, corresponding to the characters in the i-th row of the suitable board you found. Each character of a string can either be U, D, L, R or X. If there exist several different boards that satisfy the provided information, you can find any of them.

Examples
Input
2
1 1 1 1
2 2 2 2
Output
VALID
XL
RX
Input
3
-1 -1 -1 -1 -1 -1
-1 -1 2 2 -1 -1
-1 -1 -1 -1 -1 -1
Output
VALID
RRD
UXD
ULL

思路:其实本质上就是DFS+构造。但是有一个不太好处理的地方。对于(-1,-1)这种点来说,是最好弄的了,只要把它随便安置在一个循环里面就可以了。但是对于那种不是死循环的点,我们要谨慎处理。我一开始是只要这个点会走到某一个点停止,那么我从这个点开始搜索,寻找停止点相同的点。但是这样的话,有可能就会造成死循环,或者无法在目的点停靠。我们从自己就是停靠点的这种点出发,去寻找停靠点是自己的点(有点绕),这样的话,就能保证所有的点都会停靠到应该停靠的点。两种dfs,两种路径(方向是不一样的)。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;

const int maxx=1e3+100;
struct node{
	int x,y;
	node(int a,int b)
	{
		x=a,y=b;
	}
};
vector<node> p[maxx];
int vis[maxx][maxx];
char s[maxx][maxx];
int d[][2]={{1,0},{0,1},{-1,0},{0,-1}};
int n;

inline void init()
{
	memset(vis,0,sizeof(vis));
	memset(s,'\0',sizeof(s));
	for(int i=1;i<=n;i++) p[i].clear();
}
inline void Dfs(int x,int y)
{
	if(vis[x][y]) return ;
	for(int i=0;i<4;i++)
	{
		int tx=x+d[i][0];
		int ty=y+d[i][1];
		if(tx<1||tx>n||ty<1||ty>n) continue;
		if(p[tx][ty].x==p[x][y].x&&p[tx][ty].y==p[x][y].y)
		{
			if(i==0) s[x][y]='D';
			else if(i==1) s[x][y]='R';
			else if(i==2) s[x][y]='U';
			else s[x][y]='L';
			vis[x][y]=1;
			Dfs(tx,ty);
		}
	}
}
inline void dfs(int x,int y)
{
	for(int i=0;i<4;i++)
	{
		int tx=x+d[i][0];
		int ty=y+d[i][1];
		if(tx<1||tx>n||ty<1||ty>n||vis[tx][ty]) continue;
		if(p[tx][ty].x==p[x][y].x&&p[tx][ty].y==p[x][y].y)
		{
			vis[tx][ty]=1;
			if(i==0) s[tx][ty]='U';
			else if(i==1) s[tx][ty]='L';
			else if(i==2) s[tx][ty]='D';
			else s[tx][ty]='R';
			dfs(tx,ty);
		}
	}
}
int main()
{
	scanf("%d",&n);
	init();
	int x,y;
	for(int i=1;i<=n;i++)
	{
		p[i].push_back(node(0,0));
		for(int j=1;j<=n;j++)
		{
			scanf("%d%d",&x,&y);
			p[i].push_back(node(x,y));
			if(x==i&&y==j) s[i][j]='X',vis[i][j]=1;
		}
	}
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
		{
			if(p[i][j].x!=-1)
			{
				int tx=p[i][j].x;
				int ty=p[i][j].y;
				if(p[tx][ty].x!=tx||p[tx][ty].y!=ty)
				{
					cout<<"INVALID"<<endl;
					return 0;
				}
			}
		}
	}
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
		{
			if(p[i][j].x==-1&&!vis[i][j]) Dfs(i,j);
			else if(s[i][j]=='X') dfs(i,j);
		}
	}
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
		{
			if(vis[i][j]==0)
			{
				cout<<"INVALID"<<endl;
				return 0;
			}
		}
	}
	cout<<"VALID"<<endl;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++) cout<<s[i][j];cout<<endl;
	}
	return 0;
}

努力加油a啊,(o)/~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值