问题遇到的现象和发生背景
这是洛谷的题,自己在试样例和测试点一地时候都是输出的正确答案,但是在这里一个也过不了。
原题在这儿:https://www.luogu.com.cn/problem/P1518#submit
问题相关代码,请勿粘贴截图
#include<iostream>
#include<cstdio>
char dt[11][11];
int fx,fy,cx,cy;
int left = 0,right = 0,down = 0,up = 1;//左右下上
int LEFT = 0,RIGHT = 0,DOWN = 0,UP = 1;
void fmove(int x,int y)
{
if(up == 1)
{//向上移动
if(dt[x - 1][y] == '*' || x - 1 == 0)
{
left = 0,right = 1,down = 0,up = 0;
}
else
{
dt[x - 1][y] = 'F';
dt[x][y] = '.';
fx = x - 1;
fy = y;
}
}
else if(down == 1)
{//向下移动
if(dt[x + 1][y] == '*' || x + 1 == 11)
{
left = 1,right = 0,down = 0,up = 0;
}
else
{
dt[x + 1][y] = 'F';
dt[x][y] = '.';
fx = x + 1;
fy = y;
}
}
else if(left == 1)
{//向左移动
if(dt[x][y - 1] == '*' || y - 1 == 0 )
{
left = 0,right = 0,down = 0,up = 1;
}
else
{
dt[x][y - 1] = 'F';
dt[x][y] = '.';
fx = x;
fy = y - 1;
}
}
else if(right == 1)
{//向右移动
if(dt[x][y + 1] == '*' || y + 1 == 11)
{
left = 0,right = 0,down = 1,up = 0;
}
else
{
dt[x][y + 1] = 'F';
dt[x][y] = '.';
fx = x;
fy = y + 1;
}
}
}
void cmove(int x,int y)
{
if(UP == 1)
{//向上移动
if(dt[x - 1][y] == '*' || x - 1 == 0)
{
LEFT = 0,RIGHT = 1,DOWN = 0,UP = 0;
}
else
{
dt[x - 1][y] = 'C';
dt[x][y] = '.';
cx = x - 1;
cy = y;
}
}
else if(DOWN == 1)
{//向下移动
if(dt[x + 1][y] == '*' || x + 1 == 11)
{
LEFT = 1,RIGHT = 0,DOWN = 0,UP = 0;
}
else
{
dt[x + 1][y] = 'C';
dt[x][y] = '.';
cx = x + 1;
cy = y;
}
}
else if(LEFT == 1)
{//向左移动
if(dt[x][y - 1] == '*' || y - 1 == 0 )
{
LEFT = 0,RIGHT = 0,DOWN = 0,UP = 1;
}
else
{
dt[x][y - 1] = 'C';
dt[x][y] = '.';
cx = x;
cy = y - 1;
}
}
else if(RIGHT == 1)
{//向右移动
if(dt[x][y + 1] == '*' || y + 1 == 11)
{
LEFT = 0,RIGHT = 0,DOWN = 1,UP = 0;
}
else
{
dt[x][y + 1] = 'C';
dt[x][y] = '.';
cx = x;
cy = y + 1;
}
}
}
int main()
{
int flag = 0,i,j,k,count = 1;
for (i = 1;i <= 10;i++)
{
for (j = 1;j <= 10;j++)
{
dt[i][j] = getchar();
if (dt[i][j] == 'F')
{
fx = i;
fy = j;
}
if (dt[i][j] == 'C')
{
cx = i;
cy = j;
}
}
getchar();
}
for(k = 1;k<=10000;k++)
{
cmove(cx,cy);
fmove(fx,fy);
if(cx == fx&&cy == fy)
break;
if(k == 10000)
flag =1;
count++;
}
if(flag)
printf("0");
else
printf("%d",count);
}