Codeforces Round #354 (Div. 2)676D. Theseus and labyrinth
2016-05-26 04:24
316 查看
题目链接:http://codeforces.com/contest/676/problem/D
题意:
给一个n*m的迷宫,每个格子有一个字符,代表有几个门和门的方向。每秒允许两种操作,1.移动到相对应的门都通的邻节点 2.原地不动,地图上的每个门都顺时针转90°
问你从起点到终点最短距离
思路:
暴搜就行了…
flag是代表转了几次
fun是为了找到当前结点原地图能走的门
如果原地图i方向能走,那么经过flag次旋转后i+flag方向就能走,因为我们的dir数组是顺时针的,所以flag=1就相当于dir[(i+1)%4]
题意:
给一个n*m的迷宫,每个格子有一个字符,代表有几个门和门的方向。每秒允许两种操作,1.移动到相对应的门都通的邻节点 2.原地不动,地图上的每个门都顺时针转90°
问你从起点到终点最短距离
思路:
暴搜就行了…
flag是代表转了几次
fun是为了找到当前结点原地图能走的门
如果原地图i方向能走,那么经过flag次旋转后i+flag方向就能走,因为我们的dir数组是顺时针的,所以flag=1就相当于dir[(i+1)%4]
#include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> #include<math.h> #include<queue> #include<stack> #include<string> #include<vector> #include<map> #include<set> using namespace std; #define lowbit(x) (x&(-x)) typedef long long LL; const int maxn = 1005; const int inf=(1<<28)-1; char Map[maxn][maxn]; struct node { int flag,x,y,step; }; int dir[4][2]={0,1,1,0,0,-1,-1,0}; int n,m,end_x,end_y; bool mark[maxn][maxn][4]; void go(int a,int b,int c,int d,bool* ok) { ok[0]=a,ok[1]=b,ok[2]=c,ok[3]=d; } bool ok[4],ok1[4]; void fun(node cur,bool A[]) { //右 下 左 上 int x=cur.x,y=cur.y; if(Map[x][y]=='+') go(1,1,1,1,A); else if(Map[x][y]=='-') go(1,0,1,0,A); else if(Map[x][y]=='|') go(0,1,0,1,A); else if(Map[x][y]=='^') go(0,0,0,1,A); else if(Map[x][y]=='>') go(1,0,0,0,A); else if(Map[x][y]=='<') go(0,0,1,0,A); else if(Map[x][y]=='v') go(0,1,0,0,A); else if(Map[x][y]=='L') go(1,1,0,1,A); else if(Map[x][y]=='R') go(0,1,1,1,A); else if(Map[x][y]=='U') go(1,1,1,0,A); else if(Map[x][y]=='D') go(1,0,1,1,A); else if(Map[x][y]=='*') go(0,0,0,0,A); } void bfs(node cur) { queue<node>q; mark[cur.x][cur.y][0]=true; q.push(cur); while(!q.empty()) { cur=q.front(); q.pop(); fun(cur,ok); for(int i=0;i<4;++i) if(ok[i]) { node next=cur; next.x+=dir[(i+next.flag)%4][0]; next.y+=dir[(i+next.flag)%4][1]; next.step++; if(next.x<0||next.y<0||next.x>=n||next.y>=m) continue; fun(next,ok1); if(!ok1[(i+2)%4]) continue; if(mark[next.x][next.y][next.flag]) continue; mark[next.x][next.y][next.flag]=true; if(next.x==end_x&&next.y==end_y) { printf("%d\n",next.step); return ; } q.push(next); } node next=cur; next.flag++; next.step++; next.flag%=4; if(mark[next.x][next.y][next.flag]) continue; mark[next.x][next.y][next.flag]=true; q.push(next); } printf("-1\n"); } int main() { scanf("%d%d",&n,&m); for(int i=0;i<n;++i) scanf("%s",Map[i]); node cur; scanf("%d%d",&cur.x,&cur.y); cur.x--,cur.y--; scanf("%d%d",&end_x,&end_y); end_y--,end_x--; if(cur.x==end_x&&cur.y==end_y) { printf("0\n"); return 0; } cur.flag=0; cur.step=0; bfs(cur); return 0; }
相关文章推荐
- Codeforces Round #354 (Div. 2)676C. Vasya and String
- Codeforces Round #354 (Div. 2)676B. Pyramid of Glasses
- 一千行MySQL学习笔记【博客园】
- Codeforces Round #354 (Div. 2)767A. Nicholas and Permutation
- OSChina 周四乱弹 ——我一天啥都没干,光TM自杀了
- 【经典算法】迷宫找路径C#版
- Channel Rank chip bank
- MedianBlur(中值滤波/百分比滤波器)
- 【经典算法】八皇后问题C#版
- ASP.NET的Cookie和Session
- 160CM-4
- LeetCode 142. Linked List Cycle II(循环链表)
- Codeforces Beta Round #2
- Java 可变参数
- 2. SpringMVC_HelloWorld
- 采访 Gaijin Entertainment游戏Crossout,先睹为快吧
- LeetCode 308. Range Sum Query 2D - Mutable
- HDU 1864 最大报销额 (dp 01背包)
- android view从无到有的过程
- 9.3顺序容器操作