首页 > 其他分享 >Codeforces 1316 D. Nash Matrix(dfs)

Codeforces 1316 D. Nash Matrix(dfs)

时间:2023-02-03 12:38:33浏览次数:54  
标签:1316 tx ty int rep Codeforces dfs mov Nash


Codeforces 1316 D. Nash Matrix(dfs)_搜索


Codeforces 1316 D. Nash Matrix(dfs)_搜索_02

题意:

给出一个 Codeforces 1316 D. Nash Matrix(dfs)_搜索_03 的棋盘和每个棋盘位置最后能走到的位置,如果一直走不停下来就是 Codeforces 1316 D. Nash Matrix(dfs)_搜索_04 ,可以停下来就是走到的最后位置,让你输出每个位置的操作字符 ,Codeforces 1316 D. Nash Matrix(dfs)_搜索_05上下左右和 Codeforces 1316 D. Nash Matrix(dfs)_搜索_06,停在此位置。

  • 我们先找到所有可以停下的位置,那么这个位置的字符一定是
    Codeforces 1316 D. Nash Matrix(dfs)_搜索_07,然后以这个位置为终点,方向搜索找到可以停在此位置的,搜索过程填上相反方向的字符即可。
  • 然后就是处理一直走不停的,找到一个走不停的格子,如果他相邻格子也是一直不停,就可以继续搜索下去。

AC代码:

const int N = 1e3 + 5;

struct node
{
int x, y;
} a[N][N];

char g[N][N];
int n;
int mov[4][2] = {-1, 0, 1, 0, 0, -1, 0, 1};
char mc[4] = {'D', 'U', 'R', 'L'}; //反向
char mc2[4] = {'U', 'D', 'L', 'R'}; //正向

void dfs(int nx, int ny, int fx, int fy)
{
rep(i, 0, 3)
{
int tx = nx + mov[i][0];
int ty = ny + mov[i][1];
if (tx < 1 || tx > n || ty < 1 || ty > n || g[tx][ty] != '0')
continue;
if (a[tx][ty].x == fx && a[tx][ty].y == fy)
{
g[tx][ty] = mc[i];
//cout << tx << " " << ty << " " << g[tx][ty] << " " << fx << " " << fy << endl;
dfs(tx, ty, fx, fy);
}
}
}

void init()
{
rep(i, 1, n)
rep(j, 1, n)
g[i][j] = '0';
}

int main()
{
sd(n);
init();
rep(i, 1, n)
rep(j, 1, n)
sdd(a[i][j].x, a[i][j].y);
rep(i, 1, n)
{
rep(j, 1, n)
{
if (a[i][j].x == i && a[i][j].y == j)
{
g[i][j] = 'X';
dfs(i, j, i, j);
}
}
}
rep(i, 1, n)
{
rep(j, 1, n)
{
if (a[i][j].x == -1 && a[i][j].y == -1 && g[i][j] == '0')//一直走循环的情况
{
bool flag = false;
rep(k, 0, 3)
{
int tx = i + mov[k][0];
int ty = j + mov[k][1];
if (tx < 1 || tx > n || ty < 1 || ty > n || g[tx][ty] != '0')
continue;
if (a[tx][ty].x == -1 && a[tx][ty].y == -1)//前一个位置也循环了
{
g[i][j] = mc2[k];
flag = true;
break;
}
}
if (flag)
dfs(i, j, -1, -1);
}
}
}
rep(i, 1, n)
{
rep(j, 1, n)
{
if (g[i][j] == '0')
{
puts("INVALID");
return 0;
}
}
}
puts("VALID");
rep(i, 1, n)
{
rep(j, 1, n)
printf("%c", g[i][j]);
puts("");
}
return 0;
}


标签:1316,tx,ty,int,rep,Codeforces,dfs,mov,Nash
From: https://blog.51cto.com/u_15952369/6035755

相关文章

  • Codeforces1260 E Tournament(贪心)
    Description:Youareorganizingaboxingtournament,wherenboxerswillparticipate(ispowerof),andyourfriendisoneofthem.Allboxershavedifferents......
  • Codeforces 1155D Beautiful Array
    给你n个数字的数组然后还有一个x,你可以选择一段区间乘上x,输出最大子段和。用一个二维dp来做就行了AC代码:#include<cstdio>#include<cstring>#include<iostream>#include<......
  • codeforces 1257E The Contest(lis)
    题意:3堆数,要求使得第一堆的数为前缀,第三堆数为后缀,第二堆数为剩下的数,要求最少调整多少个数的位置使得要求成立。其实就是就把a1,a2,a3排个序然后拼成一个数组,问题转为一个......
  • codeforces 1257C Dominated Subarray
    题意就是找到一个最小的子区间使得这个区间中只有一个数的个数为2.AC代码:#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<vector>#inclu......
  • Codeforces1151B-Dima and a Bad XOR(构造)
    这道题真的想复杂了,作为div2的B题肯定不算难。只要构造出任意一种异或和大于1就行,如果第一列的值异或和>1,就直接全输入1即可,如果等于0,我们只要在任意一行中找到一个不等于......
  • Codeforces Round #661 (Div. 3)
    A.RemoveSmallest题意:给定一个序列,每次操作可以任选两个差的绝对值小于等于排序后计算相邻数的差,只要有大于AC代码:constintN=2e5+50;intn,m;inta[N];intmain......
  • Codeforces Round #662 (Div. 2)
    A.RainbowDash,FluttershyandChessColoring题意:有手动写几个找找规律。AC代码:intn,m;intmain(){intT;sd(T);while(T--){sd(n);pd(n/2+1);......
  • Codeforces Round #658 (Div. 2)
    ACommonSubsequence只要找到有一个相同的元素输出即可。AC代码:constintN=1010;inta[N],b[N];intans;intcnt[N];intmain(){intt;sd(t);while(t--){......
  • Codeforces Round #657 (Div. 2)
    A.AcaciusandString题意:给你一个串,你可以把换成任意字符,使得这个串最后只出现一次暴力枚举AC代码:strings;intn;stringT="abacaba";boolcheck(string&a){int......
  • Codeforces Round #656 (Div. 3)
    A.ThreePairwiseMaximums题意:给你三个正整数和,请你找到正整数和,使得,或者确定不可能找到这样的和AC代码:intmain(){intt;sd(t);while(t--){int......