CodeForces 676D代码 哪里有问题呢?

时间:2022-06-27 17:04:47

题目:

http://codeforces.com/problemset/problem/676/D

code:

#include <stdio.h>
#define MAXN 1001
#define LEFT 0
#define TOP 1
#define RIGHT 2
#define BOTTOM 3
struct block{
//left,top,right,bottom
int doors[];
int doors_num;
int rotats;
int minutes;
};
struct block labyrinth[MAXN][MAXN];
char visited[MAXN][MAXN]; struct node{
int x;
int y;
};
int top;
struct node path[MAXN*MAXN];
int N,M;
int xt,yt,xm,ym;
int min_minutes = 0x0FFFFFFF;
void init_doors(int x,int y,char c)
{
int i;
for (i=;i<;i++)
{
labyrinth[x][y].doors[i] = ;
}
labyrinth[x][y].doors_num =;
labyrinth[x][y].rotats = ;
labyrinth[x][y].minutes = 0x0FFFFFFF;
switch(c)
{
case '+':
for (i=;i<;i++)
{
labyrinth[x][y].doors[i] = ;
}
labyrinth[x][y].doors_num = ;
labyrinth[x][y].rotats = ;
break;
case '-':
labyrinth[x][y].doors[LEFT] = ;
labyrinth[x][y].doors[RIGHT] =;
labyrinth[x][y].doors_num = ;
break;
case '|':
labyrinth[x][y].doors[TOP] = ;
labyrinth[x][y].doors[BOTTOM] =;
labyrinth[x][y].doors_num = ;
break;
case '^':
labyrinth[x][y].doors[TOP] = ;
labyrinth[x][y].doors_num = ;
break;
case '>':
labyrinth[x][y].doors[RIGHT] = ;
labyrinth[x][y].doors_num = ;
break;
case '<':
labyrinth[x][y].doors[LEFT] = ;
labyrinth[x][y].doors_num = ;
break;
case 'V':
labyrinth[x][y].doors[BOTTOM] = ;
labyrinth[x][y].doors_num = ;
labyrinth[x][y].rotats = ;
break;
case 'L':
labyrinth[x][y].doors[TOP] = ;
labyrinth[x][y].doors[BOTTOM] =;
labyrinth[x][y].doors[RIGHT] = ;
labyrinth[x][y].doors_num =;
labyrinth[x][y].rotats = ;
break;
case 'R':
labyrinth[x][y].doors[TOP] = ;
labyrinth[x][y].doors[BOTTOM] =;
labyrinth[x][y].doors[LEFT] = ;
labyrinth[x][y].doors_num = ;
labyrinth[x][y].rotats = ;
break;
case 'U':
labyrinth[x][y].doors[RIGHT] = ;
labyrinth[x][y].doors[BOTTOM] =;
labyrinth[x][y].doors[LEFT] = ;
labyrinth[x][y].doors_num = ;
break;
case 'D':
labyrinth[x][y].doors[TOP] = ;
labyrinth[x][y].doors[RIGHT] =;
labyrinth[x][y].doors[LEFT] = ;
labyrinth[x][y].doors_num = ;
break;
}
}
void push(int i,int j)
{
top++;
path[top].x = i;
path[top].y = j; }
struct node pop()
{
top--;
return path[top +];
}
int get_door(int x,int y,int direction,int roates)
{
int t = roates % ;
int index = direction - t;
if (index < )
{
index += ;
} return labyrinth[x][y].doors[index]; }
void process_state(int x,int y,int cur_rotates,int cur_time)
{
int dir_y[] = {-,,,};
int dir_x[] = { ,-,,};
int i,nx,ny;
for (i=;i<;i++)
{
nx = x+dir_x[i];
ny = y+dir_y[i];
if (nx< || nx >N)
{
continue;
}
if (ny< || ny>M)
{
continue;
}
if (get_door(x,y,i,cur_rotates)&& get_door(nx,ny,(i+)%,cur_rotates))
{
if (!visited[nx][ny])
{
visited[nx][ny] =;
labyrinth[nx][ny].minutes = cur_time+;
labyrinth[nx][ny].rotats = cur_rotates;
if (nx == xm && ny == ym)
{
if (min_minutes > cur_time+)
{
min_minutes = cur_time+;
}
}else
{ push(nx,ny);
} }else
{
if (labyrinth[nx][ny].minutes >cur_time+ )
{
labyrinth[nx][ny].minutes = cur_time+;
labyrinth[nx][ny].rotats = cur_rotates;
push(nx,ny);
}
}
} }
}
int bfs()
{
int x,y,cur_rotates,i,nx,ny,t,cur_time; struct node cur;
top = -;
push(xt,yt);
labyrinth[xt][yt].minutes = ;
visited[xt][yt] =; while(top!= -)
{
cur = pop(); //left
x = cur.x;
y = cur.y;
cur_rotates = labyrinth[x][y].rotats;
cur_time = labyrinth[x][y].minutes;
t = ;
while(t < )
{
if (cur_time + t < min_minutes)
{
process_state(x,y,cur_rotates+t,cur_time+t);
}
t++; }
}
return labyrinth[xm][ym].minutes; }
int main()
{
int i,j;
char str[MAXN];
scanf("%d %d",&N,&M);
for (i=;i<=N;i++)
{
scanf("%s ",str);
for (j=;j<=M;j++)
{ init_doors(i,j,str[j-]);
visited[i][j] = ;
}
}
scanf("%d %d",&xt,&yt);
scanf("%d %d",&xm,&ym);
//check same logcation
if (xt == xm && yt == ym)
{
printf("");
return ;
}
//dfs(xt,yt,0); bfs();
if(min_minutes == 0x0FFFFFFF)
printf("-1");
else
printf("%d",min_minutes); return ;
}