题目链接HDU1010
多好的一道题,交scanf会WA,cin一发过,我WA了30+次,惊是这样的BUG,我就说我推的公式怎会错呢!!!
(如果有字体缩小的方式,我要把上面那行缩小些,先看大家WA)
可真是一道有趣的题目,首先,有这样的图推出奇偶剪枝:
0 1 0 1
1 0 1 0
0 1 0 1
1 0 1 0
可以看到相互能达到的两点间的关系,若图上值相等的两点时间也应该是能被2整除的,反之不相等就%2==1。
完整代码:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
using namespace std;
typedef long long ll;
int dir[4][2]=
{
-1, 0,
0, -1,
0, 1,
1, 0
};
int N, M, T, ex, ey;
char mp[9][9];
bool within(int x, int y) { return x>=1 && x<=N && y>=1 && y<=M; }
bool flag=false;
void dfs(int x, int y, int t)
{
if(flag) return;
if(x==ex && y==ey && t==T) { flag=true; return; }
int less_need=(T-t)-abs(x-ex)-abs(y-ey);
if( less_need<0 || (less_need&1) ) return;
for(int i=0; i<4; i++)
{
if(mp[x+dir[i][0]][y+dir[i][1]]!='X' && within(x+dir[i][0], y+dir[i][1]))
{
mp[x+dir[i][0]][y+dir[i][1]]='X';
dfs(x+dir[i][0], y+dir[i][1], t+1);
mp[x+dir[i][0]][y+dir[i][1]]='.';
if(flag) return;
}
}
return;
}
int main()
{
while(scanf("%d%d%d", &N, &M, &T)!=EOF)
{
if(!N && !M && !T) break;
int sx=0, sy=0, wall=0;
for(int i=1; i<=N; i++)
{
for(int j=1; j<=M; j++)
{
cin>>mp[i][j];
if(mp[i][j]=='S') { sx=i; sy=j; }
else if(mp[i][j]=='D') { ex=i; ey=j; }
else if(mp[i][j]=='X') wall++;
}
}
if(N*M - wall <= T) { printf("NO\n"); continue; }
flag=false;
mp[sx][sy]='X';
dfs(sx, sy, 0);
if(flag) printf("YES\n");
else printf("NO\n");
}
return 0;
}