
用的是BFS,以下是我的代码
#include
#include
#include
using namespace std;
int used[8][8];
int Move[4][2]=
{
{-1,0},{0,-1},{1,0},{0,1}
};
int N,M,T,bx,by,ex,ey;
struct Node
{
int x,y;
int count;
};
bool Dfs(int x,int y,int count)
{
if (x==ex&&y==ey)
{
if (count==T)
return true;
}
int NewX,NewY,NewCount;
stack
Node top,temp;
top.x=x,top.y=y,top.count=count;
s.push(top);
while(!s.empty())
{
top=s.top();
s.pop();
for(int i=0;i<4;i++)
{
NewX=top.x+Move[i][0];
NewY=top.y+Move[i][1];
NewCount=top.count;
if (NewX>=0&&NewX
{
used[NewX][NewY]=1;
NewCount++;
if (NewX==ex&&NewY==ey&&NewCount==T)
return true;
temp.x=NewX;
temp.y=NewY;
temp.count=NewCount;
s.push(temp);
}
}
}
return false;
}
void Input(int r,int c)
{
memset(used,0,sizeof(used));
char ch;
for(int i=0;i
for(int j=0;j
cin>>ch;
if (ch=='S')
{
bx=i;
by=j;
used[i][j]=1;
}
else if(ch=='D')
{
ex=i;
ey=j;
}
else if (ch=='X')
{
used[i][j]=1;
}
}
}
}
int main(int argc, char* argv[])
{
while(cin>>N>>M>>T)
{
if (N==0&&M==0&&T==0)
break;
Input(N,M);
if (((abs(bx-ex)+abs(by-ey))&1)==
(T&1)) //奇偶校验
{
if (Dfs(bx,by,0))
cout<<"YES"<
cout<<"NO"<
else
cout<<"NO"<
return 0;
}
各位帮帮忙吧,看下哪里错了,Wrong Answer
帮忙解释一下,贴代码什么的就不必了,万分感谢
这题目应该是用过的点还可以再用迅弊,你看下这组数据,可能是我理解的不对,咱们一起研究一下
2 2 3
DX
S.
先向右,再向左,最后向上就能出去
但是你的基昌猜代码把S标记过了,会搏型导致一直找不到路