HDU 1885 Key Task 狀態壓縮+搜尋,hdutask
點擊開啟連結Key TaskTime Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1176 Accepted Submission(s): 462
Problem DescriptionThe Czech Technical University is rather old — you already know that it celebrates 300 years of its existence in 2007. Some of the university buildings are old as well. And the navigation in old buildings can sometimes be a little bit tricky, because of strange long corridors that fork and join at absolutely unexpected places.
The result is that some first-graders have often di?culties finding the right way to their classes. Therefore, the Student Union has developed a computer game to help the students to practice their orientation skills. The goal of the game is to find the way out of a labyrinth. Your task is to write a verification software that solves this game.
The labyrinth is a 2-dimensional grid of squares, each square is either free or filled with a wall. Some of the free squares may contain doors or keys. There are four di?erent types of keys and doors: blue, yellow, red, and green. Each key can open only doors of the same color.
You can move between adjacent free squares vertically or horizontally, diagonal movement is not allowed. You may not go across walls and you cannot leave the labyrinth area. If a square contains a door, you may go there only if you have stepped on a square with an appropriate key before.
InputThe input consists of several maps. Each map begins with a line containing two integer numbers R and C (1 ≤ R, C ≤ 100) specifying the map size. Then there are R lines each containing C characters. Each character is one of the following:
Note that it is allowed to have
more than one exit,
no exit at all,
more doors and/or keys of the same color, and
keys without corresponding doors and vice versa.
You may assume that the marker of your position (“*”) will appear exactly once in every map.
There is one blank line after each map. The input is terminated by two zeros in place of the map size.
OutputFor each map, print one line containing the sentence “Escape possible in S steps.”, where S is the smallest possible number of step to reach any of the exits. If no exit can be reached, output the string “The poor student is trapped!” instead.
One step is defined as a movement between two adjacent cells. Grabbing a key or unlocking a door does not count as a step.
Sample Input
1 10*........X1 3*#X3 20#####################XY.gBr.*.Rb.G.GG.y#####################0 0
Sample Output
Escape possible in 9 steps.The poor student is trapped!Escape possible in 45 steps.
每個位置有16種狀態,可以用一個vis三維數組表示每個點的16種狀態,對於四種鎖,每種鎖用一位來表示,有鑰匙標記為1,否則標記為0.然後bfs搜尋一下就行了。
//109MS556K#include<stdio.h>#include<queue>#include<string.h>#include<algorithm>#define M 107using namespace std;int n,m,s,t;int dir[4][2]={{-1,0},{1,0},{0,1},{0,-1}};char g[M][M];bool vis[M][M][17];char up[4]={'B','Y','R','G'};char low[4]={'b','y','r','g'};struct node{ int step,x,y,key;};int bfs(){ queue<node>q; node now,next; now.x=s;now.y=t;now.step=now.key=0; vis[s][t][0]=true; q.push(now); while(!q.empty()) { now=q.front(); q.pop(); if(g[now.x][now.y]=='X')return now.step; for(int i=0;i<4;i++) { next.x=now.x+dir[i][0]; next.y=now.y+dir[i][1]; next.step=now.step+1; next.key=now.key; if(next.x<1||next.x>n||next.y<1||next.y>m||g[next.x][next.y]=='#')continue;//如果越界 if(g[next.x][next.y]>='A'&&g[next.x][next.y]<='Z'&&g[next.x][next.y]!='X') { for(int j=0;j<4;j++) if(g[next.x][next.y]==up[j]) { if(next.key&(1<<j)&&!vis[next.x][next.y][next.key])//如果沒有訪問過且擁有此鎖的鑰匙 { vis[next.x][next.y][next.key]=true; q.push(next); } break; } } else if(g[next.x][next.y]>='a'&&g[next.x][next.y]<='z') { for(int j=0;j<4;j++) if(g[next.x][next.y]==low[j]) { if((next.key&(1<<j))==0)//如果沒有此鑰匙 next.key+=(1<<j); if(!vis[next.x][next.y][next.key]) { vis[next.x][next.y][next.key]=true; q.push(next); } } } else { if(!vis[next.x][next.y][next.key]) { vis[next.x][next.y][next.key]=true; q.push(next); } } } } return -1;}int main(){ while(scanf("%d%d",&n,&m),n|m) { memset(vis,false,sizeof(vis)); for(int i=1;i<=n;i++) { scanf("%s",g[i]+1); for(int j=1;j<=m;j++) if(g[i][j]=='*'){s=i;t=j;} } int ans=bfs(); if(ans==-1)printf("The poor student is trapped!\n"); else printf("Escape possible in %d steps.\n",ans); } return 0;}
ACM解題報告
一個POJ題目分類
主流演算法:
1.搜尋 //回溯
2.DP(動態規劃)
3.貪心
4.圖論 //Dijkstra、最小產生樹、網路流
5.數論 //解模線性方程
6.計算幾何 //凸殼、同等安置矩形的並的面積與周長
7.組合數學 //Polya 定理
8.類比
9.資料結構 //並查集、堆
10.博弈論
1、 排序
1423, 1694, 1723, 1727, 1763, 1788, 1828, 1838, 1840, 2201, 2376,
2377, 2380, 1318, 1877,1928, 1971, 1974, 1990, 2001, 2002, 2092, 2379,
1002(需要字元處理,排序用快排即可) 1007(穩定的排序) 2159(題意較難懂)
2231 2371(簡單排序) 2388(順序統計演算法) 2418(二叉排序樹)
2、 搜尋、回溯、遍曆
1022,1111,1118,1129,1190,1562,1564,1573,1655,2184 ,2225,2243,2312,2362,2378,2386,1010,1011,1018,1020,1054,1062,1256,1321,1363,1501,1650,1659,1664,1753,2078,2083,2303,2310,2329
簡單:1128, 1166, 1176, 1231, 1256, 1270, 1321, 1543, 1606, 1664,
1731, 1742, 1745, 1847,1915, 1950, 2038, 2157, 2182, 2183, 2381, 2386, 2426
不易:1024, 1054, 1117, 1167, 1708, 1746, 1775, 1878, 1903, 1966, 2046,
2197, 2349
推薦:1011, 1190, 1191, 1416, 1579, 1632, 1639, 1659, 1680, 1683, 1691,
1709, 1714, 1753,1771, 1826, 1855, 1856, 1890, 1924, 1935, 1948, 1979, 1980, 2170,2288, 2331, 2339,2340,1979(和迷宮類似),1980(對剪枝要求較高)
3、 曆法
1008 2080 (這種題要小心)
4、 枚舉
1012,1046, 1387, 1411, 2245, 2326, 2363, 2381,1054(剪枝要求較高),1650 (小數的精度問題)
5、 資料結構的典型演算法
容易:1182, 1656, 2021, 2023, 2051, 2153, 2227, 2236, 2247, 2352,
2395
不易:1145, 1177, 1195, 1227, 1661, 1834
推薦:1330, 1338, 1451, 1470, 1634, 1689, 1693, 1703, 1724, 1988, 2004,
2010, 2119, 2274
1125(弗洛伊德演算法) ,2421(圖的最小產生樹)
6、 動態規劃
1037 A decorative fence、
1050 To the Max、
1088 滑雪、
1125 Stockbroker Grapevine、
1141 Brackets Sequence、
1159 Palindrom......餘下全文>>