poj3009

错误的把 

int ax = x + dir[i][0];

int ay = y + dir[i][1];

写成了 

 int ax = x + dir[i][0];
int ay = y + dir[i][0];

注意点: 

 int ax = x + dir[i][0];
 int ay = y + dir[i][1];

不能写成

 x = x + dir[i][0];
 y

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int Map[30][30];
int dir[][2] = {{1,0},{-1, 0},{0, 1},{0,-1}};
int ans;
int ex, ey;
int n, m;


bool check(int x, int y){

  if(x >= 0 && x < n && y >= 0 && y < m)
    return true;;
  return false;
}

void dfs(int cnt, int x, int y) {

    if(cnt >= 10)
        return;

    for(int i = 0; i < 4; i++){

       int ax = x + dir[i][0];
       int ay = y + dir[i][1];

        if(Map[ax][ay] == 1){

            continue;
        }
        while(!Map[ax][ay]){

            ax += dir[i][0]; //在这里我犯了一个重大的失误,写成 x += dir[i][0]
                             //改变了x的值, 使后面出现错误。
            ay += dir[i][1];
        }

        if(check(ax, ay)){

            if(Map[ax][ay] == 1){

                Map[ax][ay] = 0;
              dfs(cnt+1, ax - dir[i][0], ay - dir[i][1]);
              Map[ax][ay] = 1;
            }
            if(Map[ax][ay] == 3){
                   // printf("%d  %d\n", ans, cnt + 1);
                ans = min(ans, cnt+1);
            }
        }
    }
}

int main()
{
    int sx, sy;
    while(scanf("%d%d", &m, &n) != EOF) {

        if(!n && !m)
            break;
        for(int i = 0; i < n; i++)
         for(int j = 0; j < m; j++){

            scanf("%d", &Map[i][j]);
            if(Map[i][j] == 2){

                sx = i;
                sy = j;
                Map[i][j] = 0;
            }
            if(Map[i][j] == 3){

                ex = i;
                ey = j;
            }
        }
        ans = 11;
        dfs(0, sx, sy);
        if(ans < 11)
        printf("%d\n", ans);
        else
            printf("-1\n");
    }
    return 0;
}

 = y + dir[i][1];


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值