UVA10047独轮车

题意:
      给你一个独轮车,轮子上有五个扇形,每过一个格子就转过一个扇形,刚开始的时候方向是向北的,绿色上行向下,每一次可以有三种操作,到下一个格子,左转90度,右转90度,每一次操作都花费时间1,问从起点到终点的最小步数,要求到终点的时候必须还是绿色扇形向下,方向无所谓。


思路:
      mark[x][y][col][fx]代表的是在点(x,y)处,颜色是col方向是fx是否走过,然后就是个简单的广搜了,还有提示下,这个题目没有必要用优先队列的,每次操作花费都一样,仔细想想队列里面的东西,本来就是按照步数小的在前面存的,具体细节看代码,还有个事就是UVA上貌似没有pe啊!,pe貌似是直接wa的节奏。


#include<queue>
#include<stdio.h>
#include<string.h>

using namespace std;

typedef struct
{
   int x ,y ,fx ,col ,t;
}NODE;

NODE xin ,tou;
int mark[26][26][6][5];
int map[26][26];
int ex ,ey ,sx ,sy;
int n ,m;

bool ok(int x, int y ,int c ,int f)
{
   return x >= 1 && x <= n && y >= 1 && y <= m && !map[x][y] && !mark[x][y][c][f];
}


int BFS()
{
   xin.x = sx ,xin.y = sy ,xin.t = 0 ,xin.fx = 1 ,xin.col = 1;
   memset(mark ,0 ,sizeof(mark));
   mark[sx][sy][1][1] = 1;
   queue<NODE>q;
   q.push(xin);
   while(!q.empty())
   {
      tou = q.front();
      q.pop();
      if(tou.x == ex && tou.y == ey && tou.col == 1) 
      return tou.t;
      
      //向前
      if(tou.fx == 1) xin.x = tou.x - 1 ,xin.y = tou.y;
      if(tou.fx == 2) xin.x = tou.x ,xin.y = tou.y + 1;
      if(tou.fx == 3) xin.x = tou.x + 1 ,xin.y = tou.y;
      if(tou.fx == 4) xin.x = tou.x ,xin.y = tou.y - 1;
      xin.fx = tou.fx ,xin.t = tou.t + 1 ,xin.col = (tou.col + 1) % 6;
      if(!xin.col) xin.col ++;
      if(ok(xin.x ,xin.y ,xin.col ,xin.fx))
      {
         mark[xin.x][xin.y][xin.col][xin.fx] = 1;
         q.push(xin);
      }
      //向右
      xin.x = tou.x ,xin.y = tou.y; 
      xin.t = tou.t + 1 ,xin.col = tou.col;
      xin.fx = tou.fx + 1;
      if(xin.fx > 4) xin.fx = 1;
      if(ok(xin.x ,xin.y ,xin.col ,xin.fx))
      {
         mark[xin.x][xin.y][xin.col][xin.fx] = 1;
         q.push(xin);
      } 
      //向左 
      xin.x = tou.x ,xin.y = tou.y; 
      xin.t = tou.t + 1 ,xin.col = tou.col;
      xin.fx = tou.fx - 1;
      if(xin.fx == 0) xin.fx = 4;
      if(ok(xin.x ,xin.y ,xin.col ,xin.fx))
      {
         mark[xin.x][xin.y][xin.col][xin.fx] = 1;
         q.push(xin);
      } 
   }
   return -1;
}


int main ()
{
   int i ,j ,Ans ,cas = 1 ,mk = 0;
   char str[30];
   while(~scanf("%d %d" ,&n ,&m) && n + m)
   {
      for(i = 1 ;i <= n ;i ++)
      {
         scanf("%s" ,str);
         for(j = 0 ;j < m ;j ++)
         {
            if(str[j] == '#') map[i][j+1] = 1;
            if(str[j] == '.') map[i][j+1] = 0;
            if(str[j] == 'S') map[i][j+1] = 0 ,sx = i ,sy = j + 1;
            if(str[j] == 'T') map[i][j+1] = 0 ,ex = i ,ey = j + 1;
         }
      }
      
      Ans = BFS();                  
      if(mk) puts("");
      printf("Case #%d " ,cas ++);
      Ans == -1 ? puts("destination not reachable"):printf("minimum time = %d sec " ,Ans);
      mk = 1;
   }
   return 0;
}
      
         

      
      













原文地址:https://www.cnblogs.com/csnd/p/12062687.html