Tempter of the Bone【DFS+奇偶剪枝】scanf会WA!!!

题目链接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;
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wuliwuliii

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值