程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 1885Key Task£¨BFS+״̬ѹËõ£©£¨Ê¤Àû´óÌÓÍöÐø£©Ò»¸öÒâ˼

1885Key Task£¨BFS+״̬ѹËõ£©£¨Ê¤Àû´óÌÓÍöÐø£©Ò»¸öÒâ˼

編輯:C++入門知識

1885Key Task£¨BFS+״̬ѹËõ£©£¨Ê¤Àû´óÌÓÍöÐø£©Ò»¸öÒâ˼


Key Task

Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1229 Accepted Submission(s): 493


Problem Description The 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.
Input The 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,
mZ†·Ÿ"http://www.Bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcmUgZG9vcnMgYW5kL29yIGtleXMgb2YgdGhlIHNhbWUgY29sb3IsIGFuZCA8YnI+CmtleXMgd2l0aG91dCBjb3JyZXNwb25kaW5nIGRvb3JzIGFuZCB2aWNlIHZlcnNhLiA8YnI+Cjxicj4KWW91IG1heSBhc3N1bWUgdGhhdCB0aGUgbWFya2VyIG9mIHlvdXIgcG9zaXRpb24gKKGwKqGxKSB3aWxsIGFwcGVhciBleGFjdGx5IG9uY2UgaW4gZXZlcnkgbWFwLgo8YnI+Cjxicj4KVGhlcmUgaXMgb25lIGJsYW5rIGxpbmUgYWZ0ZXIgZWFjaCBtYXAuIFRoZSBpbnB1dCBpcyB0ZXJtaW5hdGVkIGJ5IHR3byB6ZXJvcyBpbiBwbGFjZSBvZiB0aGUgbWFwIHNpemUuCgogCjxicj4KT3V0cHV0CkZvciBlYWNoIG1hcCwgcHJpbnQgb25lIGxpbmUgY29udGFpbmluZyB0aGUgc2VudGVuY2UgobBFc2NhcGUgcG9zc2libGUgaW4gUyBzdGVwcy6hsSwgd2hlcmUgUyBpcyB0aGUgc21hbGxlc3QgcG9zc2libGUgbnVtYmVyIG9mIHN0ZXAgdG8gcmVhY2ggYW55IG9mIHRoZSBleGl0cy4gSWYgbm8gZXhpdCBjYW4gYmUgcmVhY2hlZCwgb3V0cHV0IHRoZSBzdHJpbmcgobBUaGUgcG9vciBzdHVkZW50IGlzIHRyYXBwZWQhobEKIGluc3RlYWQuIDxicj4KPGJyPgpPbmUgc3RlcCBpcyBkZWZpbmVkIGFzIGEgbW92ZW1lbnQgYmV0d2VlbiB0d28gYWRqYWNlbnQgY2VsbHMuIEdyYWJiaW5nIGEga2V5IG9yIHVubG9ja2luZyBhIGRvb3IgZG9lcyBub3QgY291bnQgYXMgYSBzdGVwLgoKIAo8YnI+ClNhbXBsZSBJbnB1dAoKPHByZSBjbGFzcz0="brush:java;">1 10 *........X 1 3 *#X 3 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.

ÿ¸ö格×Ó¶¼Óв»Í¬µÄ״̬£¬¹Ø¼üÊÇÔÚÅжÏÔÚ×ßµ½µ±Ç°格×ӵĵ±Ç°key״̬ÊÇ·ñ×ß¹ý¡£

#include
#include
#include
#include
using namespace std;
#define N 105
struct locate
{
    int x,y,step,state;
};
char map[N][N];
int state[N][N][16],n,m;

int BFS(int sx,int sy)
{
    queueq;
    locate p,tp;
    int dir[4][2]={0,1,1,0,0,-1,-1,0};
    char door[6]={"BYRG"},key[6]={"byrg"};

    memset(state,0,sizeof(state));
    p.step=0; p.state=0;
    p.x=sx; p.y=sy;
    q.push(p); state[sx][sy][0]=1;

    while(!q.empty())
    {
        p=q.front(); q.pop();
        for(int e = 0;e < 4;++e)
        {
            tp.x=p.x+dir[e][0];
            tp.y=p.y+dir[e][1];

            if(tp.x>=0&&tp.x=0&&tp.y0)
    {
        if(n==0&&m==0)
            break;
        for(int i=0;i

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved