MZOJ 1345 hero

一道宽搜模版题,可写错了两个地方的我只得了56(掩面痛哭)

http://10.37.2.111/problem.php?id=1345

先看看正确的

#include <bits/stdc++.h>
#define read read()
#define up(i,l,r) for(int i = l; i<=r; i++)
using namespace std;

int read
{
    int x = 0; char ch = getchar();
    while(ch < 48 || ch > 57)  ch = getchar();
    while(ch >= 48 && ch <= 57) {x = 10 * x + ch - 48; ch = getchar();}
    return x;
}

const int N = 105;

int n,m,k;
int sx,sy,ex,ey;
int ans = INT_MAX; 
int dx[] = {0,1,-1, 0,0};//上,下,左,右; 
int dy[] = {0,0, 0,-1,1};
struct node{int x,y,step;}st;
int th[N][N],vis[N][N];
int nx,ny;

void updata(int i,int p)
{
    if(i==1) (nx += p) > n ?nx = n : nx;     //
    else if(i==2) (nx -= p) < 1 ?nx = 1 : nx;//
    else if(i==3) (ny -= p) < 1 ?ny = 1 : ny;//
    else if(i==4) (ny += p) > m ?ny = m : ny;//
}

void bfs()
{
    queue<node>q;
    st.step = 0;
    st.x = sx;
    st.y = sy;
    q.push(st);
    vis[sx][sy]=1;
    while(!q.empty())
    {
        node cur = q.front(); q.pop();
        if(cur.x == ex && cur.y == ey) {printf("%d",cur.step); exit(0);}
        up(i,1,4)
        {
            nx = cur.x + dx[i];
            ny = cur.y + dy[i];
       if(nx < 1|| ny < 1 || nx > n || ny > m || vis[nx][ny]) continue;
            vis[nx][ny] = 1;
            node nxt; nxt.x = nx; nxt.y = ny; nxt.step = cur.step + 1;
            q.push(nxt);
        }
    }
    printf("Impossible");
}

int main()
{
    freopen("hero2.in","r",stdin);
    n = read; m = read; k = read;
    int x,y;
    int cnt = 0;
    up(i,1,k)
    {
        x = read; y = read; th[x][y] = read;
    }
    sx = read; sy = read;
    ex = read; ey = read;
    bfs();
}

错误1:代码笔误;

void updata(int i,int p)
{
    if(i==1) (nx += p) > n ?n : nx;     //
    else if(i==2) (nx -= p) < 1 ?1 : nx;//
    else if(i==3) (ny -= p) < 1 ?1 : ny;//
    else if(i==4) (ny += p) > m ?m : ny;//
}

看出什么了吗? -> 忘了赋值了;

错误2:思维漏洞;

if(th[nx][ny]) updata(i,th[nx][ny]);

弹簧可以连续跳啊连续跳 -> 所以要多次更新

记住教训啊;

猜你喜欢

转载自www.cnblogs.com/mzg1805/p/10316175.html