codeforces 1316D - Nash Matrix ,思维构造+搜索(dfs好题)

D. Nash Matrix

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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×nn×n board. Rows and columns of this board are numbered from 11 to nn. The cell on the intersection of the rr-th row and cc-th column is denoted by (r,c)(r,c).

Some cells on the board are called blocked zones. On each cell of the board, there is written one of the following 55 characters  — UU, DD, LL, RR or XX  — instructions for the player. Suppose that the current cell is (r,c)(r,c). If the character is RR, the player should move to the right cell (r,c+1)(r,c+1), for LL the player should move to the left cell (r,c−1)(r,c−1), for UU the player should move to the top cell (r−1,c)(r−1,c), for DD the player should move to the bottom cell (r+1,c)(r+1,c). Finally, if the character in the cell is XX, 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 n2n2 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)(r,c) she wrote:

  • a pair (xx,yy), meaning if a player had started at (r,c)(r,c), he would end up at cell (xx,yy).
  • or a pair (−1−1,−1−1), meaning if a player had started at (r,c)(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 nn (1≤n≤1031≤n≤103)  — the side of the board.

The ii-th of the next nn lines of the input contains 2n2n integers x1,y1,x2,y2,…,xn,ynx1,y1,x2,y2,…,xn,yn, where (xj,yj)(xj,yj) (1≤xj≤n,1≤yj≤n1≤xj≤n,1≤yj≤n, or (xj,yj)=(−1,−1)(xj,yj)=(−1,−1)) is the pair written by Alice for the cell (i,j)(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 ii-th of the next nn lines, print the string of nn characters, corresponding to the characters in the ii-th row of the suitable board you found. Each character of a string can either be UU, DD, LL, RR or XX. 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

 

 

题意:给你一个n*n的图,给你五种选择,U,W,L,R,X,分别代表可以上下左右走和不能走,然后给你每一个点的终点的位置,让你构造一种答案使得满足所有条件。

思路:其实一想就是个搜索题啊,首先,X就是终点,X可以确定X周围四个点的状态,如果周围四个点的终点是X,那么这个点的状态就确定了,同理,以确定的这个点周围四个位置的终点如果也为X(注意此时终点不会变,如果a到x,b到a,那么b能到x,所以b的终点是x),所以根据这个将所有X能确定的点先dfs确定出来,我dfs有点弱,写的很费劲,然后就是-1了,其实-1的点很好办,如果-1四个位置有-1则让他指向那个,然后还是dfs就行了。具体看代码吧

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxx=1e3+12;
inline ll read(){ll s=0,w=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;}
ll dx[4]={1,-1,0,0};
ll dy[4]={0,0,1,-1};
// 上下左右.
char c[4]={'D','W','R','L'};
char cc[4]={'U','D','L','R'};
ll n,m;
char a[maxx][maxx];
struct stu
{
	ll x,y;
}A[maxx][maxx];
void dfs(ll posx,ll posy,ll x,ll y)  //  起点。 终点 
{
	for(int i=0;i<4;i++)
	{
		ll l=posx+dx[i],r=posy+dy[i];
		if(l>=1&&l<=n&&r>=1&&r<=n&&a[l][r]=='0') // 他是不确定的点 
		{
		//	printf("%lld  %lld  %c\n",l,r,cc[i]);
			if(A[l][r].x==x&&A[l][r].y==y)
			{
				a[l][r]=cc[i];
				
				dfs(l,r,x,y);  // 它的终点是不会变的. 
			}
		}
	}
}
int main()
{
    //  board
   	ll t,p,i,j,k;
   	cin>>n;
  	for(i=1;i<=n;i++)
	{
		for(j=1;j<=n;j++)
		{
			A[i][j].x=read();
			A[i][j].y=read();
			//printf("%d %d %d %d\n",i,j,A[i][j].x,A[i][j].y);
			if(A[i][j].x==i&&A[i][j].y==j)
			a[i][j]='X';  // 确定的点 
			else a[i][j]='0';// 不确定 
		}
	}
   	for(i=1;i<=n;i++)
   	{
   		for(j=1;j<=n;j++)
		{
			if(a[i][j]=='X')
			{
			//	printf("%d %d\n",i,j);
				dfs(i,j,i,j);	
			}	
		}	
	}
	// 处理完所有的应该只剩下-1,-1的了 
	//如果他能无限走的话,那么他所走过的点都是无限制的走,所以直接让他走过去就行了。 
	for(i=1;i<=n;i++)
		for(j=1;j<=n;j++)
		{
			int flag=0;
			if(A[i][j].x==-1&&A[i][j].y==-1&&a[i][j]=='0')
			{
				
				for(k=0;k<4;k++)
				{
					ll l=i+dx[k],r=j+dy[k];
					if(l>=1&&l<=n&&r>=1&&r<=n&&A[l][r].x==-1&&A[l][r].y==-1)
					{
						a[i][j]=c[k];
						flag=1;
						break; 
					}
				}
			}
			if(flag==1) dfs(i,j,-1,-1);
		}
	int flag=0;
	for(i=1;i<=n;i++)
   	for(j=1;j<=n;j++)
   	{
   		if(a[i][j]=='0') flag=1;	
	}
	if(flag==0)
	{
		printf("VALID\n");
		for(i=1;i<=n;i++)
		{
			for(j=1;j<=n;j++)
		   	{
		   		printf("%c",a[i][j]);
			}
			printf("\n");
		}
	}
	else printf("INVALID\n");
   	
   	
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值