HDU 4740 The Donkey of Gui Zhou (模拟)

来源:互联网 发布:c语言库函数是什么 编辑:程序博客网 时间:2024/05/16 19:41

由于一开始考虑的很不周到,找到很多bug.....越改越长,不忍直视。 不是写模拟的料......................


反正撞墙或者碰到已经走过的点就会转向,转向后还碰到这两种情况就会傻站那不动了......


#include <iostream>#include <algorithm>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <string>#include <vector>#include <set>#include <queue>#include <stack>#include <climits>//形如INT_MAX一类的#define MAX 1111using namespace std;int vis1[MAX][MAX];int vis2[MAX][MAX];int step1[MAX][MAX];int step2[MAX][MAX];int dx[] = {0,1,0,-1};int dy[] = {1,0,-1,0};struct node {    int x,y,dir,kind;} q[1011111],st,end,ans,stop;int head,tail,n,yes;int flag1 , flag2;bool inside(int x,int y) {    if(x >= 0 && x < n && y >= 0 && y < n) return true;    return false;}bool ok(int x,int y,int kind) {    if(kind == 1 && vis1[x][y] == 0) return true;    if(kind == 2 && vis2[x][y] == 0) return true;    return false;}void init() {    memset(vis1,0,sizeof(vis1));    memset(vis2,0,sizeof(vis2));    memset(step1,-1,sizeof(step1));    memset(step2,-1,sizeof(step2));    head = 0;    tail = 0;    yes = 0;    flag1 = 0;    flag2 = 0;    stop.x = -1;    stop.y = -1;}void getstop(node t,node tt) {    stop.x = t.x;    stop.y = t.y;    if(tt.kind == 1) flag1 = 1,stop.kind = 1;    if(tt.kind == 2) flag2 = 1,stop.kind = 2;}void bfs() {    vis1[st.x][st.y] = 1;    vis2[end.x][end.y] = 1;    step1[st.x][st.y] = 0;    step2[end.x][end.y] = 0;    q[head++] = st;    q[head++] = end;    while(head != tail) {        node t = q[tail++];        //cout << t.x << ' ' << t.y << endl;        if(step1[t.x][t.y] == step2[t.x][t.y] && step1[t.x][t.y] != -1) {            ans.x = t.x;            ans.y = t.y;            yes = 1;            return ;        }        if(t.x == stop.x && t.y == stop.y && t.kind != stop.kind) {            ans.x = t.x;            ans.y = t.y;            yes = 1;            return ;        }        if(flag1 == 1 && flag2 == 1) {            ans.x = -1;            return ;        }        node tt = t;        tt.x = t.x + dx[t.dir];        tt.y = t.y + dy[t.dir];        if(inside(tt.x,tt.y)) {            if(ok(tt.x,tt.y,tt.kind)) {                if(tt.kind == 1) vis1[tt.x][tt.y] = 1, step1[tt.x][tt.y] = step1[t.x][t.y] + 1;                if(tt.kind == 2) vis2[tt.x][tt.y] = 1, step2[tt.x][tt.y] = step2[t.x][t.y] + 1;                q[head++] = tt;            } else {                if(tt.kind == 1) {                    if(tt.dir == 3) tt.dir = 0;                    else tt.dir ++;                    tt.x = t.x + dx[tt.dir];                    tt.y = t.y + dy[tt.dir];                    if(vis1[tt.x][tt.y] == 0) {                        vis1[tt.x][tt.y] = 1;                        step1[tt.x][tt.y] = step1[t.x][t.y] + 1;                        q[head++] = tt;                    } else getstop(t,tt);                }                if(tt.kind == 2) {                    if(tt.dir == 0) tt.dir = 3;                    else tt.dir --;                    tt.x = t.x + dx[tt.dir];                    tt.y = t.y + dy[tt.dir];                    if(vis2[tt.x][tt.y] == 0) {                        vis2[tt.x][tt.y] = 1;                        step2[tt.x][tt.y] = step2[t.x][t.y] + 1;                        q[head++] = tt;                    } else getstop(t,tt);                }            }        } else {            if(tt.x < 0) {                if(tt.kind == 1) {                    tt.dir = 0;                    tt.x = t.x + dx[0];                    tt.y = t.y + dy[0];                } else {                    tt.dir = 2;                    tt.x = t.x + dx[2];                    tt.y = t.y + dy[2];                }            } else if(tt.x >= n) {                if(tt.kind == 1) {                    tt.dir = 2;                    tt.x = t.x + dx[2];                    tt.y = t.y + dy[2];                } else {                    tt.dir = 0;                    tt.x = t.x + dx[0];                    tt.y = t.y + dy[0];                }            } else if(tt.y < 0) {                if(tt.kind == 1) {                    tt.dir = 3;                    tt.x = t.x + dx[3];                    tt.y = t.y + dy[3];                } else {                    tt.dir = 1;                    tt.x = t.x + dx[1];                    tt.y = t.y + dy[1];                }            } else if(tt.y >= n) {                if(tt.kind == 1) {                    tt.dir = 1;                    tt.x = t.x + dx[1];                    tt.y = t.y + dy[1];                } else {                    tt.dir = 3;                    tt.x = t.x + dx[3];                    tt.y = t.y + dy[3];                }            }            if(inside(tt.x,tt.y)) {                if(ok(tt.x,tt.y,tt.kind)) {                    if(tt.kind == 1) vis1[tt.x][tt.y] = 1, step1[tt.x][tt.y] = step1[t.x][t.y] + 1;                    if(tt.kind == 2) vis2[tt.x][tt.y] = 1, step2[tt.x][tt.y] = step2[t.x][t.y] + 1;                    q[head++] = tt;                } else getstop(t,tt);            } else getstop(t,tt);        }    }}int main() {    while(scanf("%d",&n) && n) {        init();        scanf("%d%d%d",&st.x,&st.y,&st.dir);        st.kind = 1;        scanf("%d%d%d",&end.x,&end.y,&end.dir);        end.kind = 2;        bfs();        if(ans.x == -1 || yes == 0) {            printf("-1\n");        } else {            printf("%d %d\n",ans.x,ans.y);        }    }    return 0;}


原创粉丝点击