标签:des blog http io ar os sp for strong
Description
Input
Output
Sample Input
Sample Output
/* *Author: Ouc_Sky *Problom: E(Shanghai2014) *Algorithm: zhuangya dp *Time: 2014/11/20 */ #include<stdio.h> #include<string.h> #include<queue> #include<algorithm> #define debug 0 using namespace std; const int MAXN = 52; struct node { int x,y,key,step; }; int n,m,p,k,s; int ans; int gate[MAXN][MAXN][MAXN][MAXN]; int key[MAXN][MAXN]; bool vis[MAXN][MAXN][1<<11]; int dx[]={1,-1,0,0}; int dy[]={0,0,1,-1}; //right left down up int main() { while(~scanf("%d%d%d",&n,&m,&p)) { memset(gate,-1,sizeof(gate)); memset(key,0,sizeof(key)); memset(vis,false,sizeof(vis)); ans = -1; scanf("%d",&k); for(int i=0;i<k;i++) { int x1,y1,x2,y2,gi; scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&gi); gate[x1][y1][x2][y2] = gi; gate[x2][y2][x1][y1] = gi; } if (debug) printf("input2 Yes\n"); scanf("%d",&s); for(int i=0;i<s;i++) { int x,y,q; scanf("%d%d%d",&x,&y,&q); key[x][y] = (1<<(q-1)) | key[x][y]; } queue<node> que; struct node next = {1,1,0,0}; que.push(next); while(!que.empty()) { struct node now = que.front(); que.pop(); if(now.x == n && now.y == m) { ans = now.step; break; } now.key = key[now.x][now.y] | now.key; for(int i=0;i<4;i++) { next.x = now.x+dx[i]; next.y = now.y+dy[i]; if(now.x >= 1 && now.x <= n && now.y >= 1 && now.y <= m) { int thisGate = gate[now.x][now.y][next.x][next.y]; if( thisGate > 0 && (now.key & (1<<(thisGate-1) )) || (thisGate == -1)) { if (!vis[next.x][next.y][now.key]) { vis[next.x][next.y][now.key] = true; next.key = now.key; next.step = now.step +1; que.push(next); } } } } } printf("%d\n",ans); }//<end of while(!scanf)> return 0; }
标签:des blog http io ar os sp for strong
原文地址:http://www.cnblogs.com/a972290869/p/4111284.html