HDU 2612

来源:互联网 发布:人伤查勘岗 知乎 编辑:程序博客网 时间:2024/06/08 15:00

还有某个人到不了的KFC……

 

 

#include "stdio.h"#include "string.h"#include "stdlib.h"#define M 201#define MM ((M)*(M))int n, m;char map[M][M];int mark[M][M];typedef struct _Time{    int ta, tb;}Time, *pTime;typedef struct _Node{    int x, y;    int t;}Node, *pNode;typedef struct _KFCLoc{    int x, y;    struct _KFCLoc *next;}KFCLoc, *pKFCLoc;Time reach[M][M];pKFCLoc kfcLoc;int ax, ay, bx, by;int dir[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};Node que[MM];int fro, end;int empty(){     return fro == end; }int full(){     return (end + 1)%MM == fro; };void push(Node e){    if(full()) return;    que[end] = e;    end = (end + 1)%MM;}Node pop(){    Node t = {-1, -1};    if(!empty()){        t = que[fro];        fro = (fro + 1)%MM;    }    return t;}void BFS(Node start, int w){    int i;    Node k;    Node z;    int nu, nv;    memset(mark, 0, sizeof(mark));    mark[start.x][start.y] = 1;    push(start);    while(!empty()){        k = pop();        for(i=0; i<4; i++){            nu = k.x + dir[i][0];            nv = k.y + dir[i][1];            if(!(nu>=0 && nu<n && nv>=0 && nv<m && map[nu][nv]!='#' && !mark[nu][nv])) continue;            mark[nu][nv] = 1;            if(map[nu][nv] == '@'){                (w ? reach[nu][nv].tb : reach[nu][nv].ta) = k.t + 1;            }            z.x = nu; z.y = nv; z.t = k.t + 1;            push(z);                }    }}void main(){    int i, j;    pKFCLoc p, t;    Node start;    int min;freopen("in.txt", "r", stdin);    while(scanf("%d %d", &n, &m) != EOF){        kfcLoc = 0;        getchar();        for(i=0; i<n; i++){            for(j=0; j<m; j++){                scanf("%c", &map[i][j]);                if(map[i][j] == '@'){                    p = (pKFCLoc)malloc(sizeof(KFCLoc));                    p->x = i; p->y = j;                    p->next = kfcLoc;                    kfcLoc = p;                }else if(map[i][j] == 'Y'){                    ax = i; ay = j;                }else if(map[i][j] == 'M'){                    bx = i; by = j;                }            }            getchar();        }memset(reach, -1, sizeof(reach));        start.x = ax; start.y = ay; start.t = 0;        BFS(start, 0);        start.x = bx; start.y = by; start.t = 0;        BFS(start, 1);        min = 0x7FFFFFFF;        while(p){            t = p;            if(reach[p->x][p->y].ta != -1 && reach[p->x][p->y].tb != -1 && (reach[p->x][p->y].ta + reach[p->x][p->y].tb < min))                min = reach[p->x][p->y].ta + reach[p->x][p->y].tb;            p = p->next;            free(t);        }        printf("%d\n", min*11);    }}


 

原创粉丝点击