o.boj 1301 Game

注:最近这一系列ACM的内容,都是2年多之前的代码,自己回顾一下。
 
 
Game
 
Submit: 356   Accepted:119
Time Limit: 1000MS  Memory Limit: 65535K
Description
最近北邮人论坛的闪客帝国里出现了一个游FLASH的小游戏,贴子的名字叫:【求助帖】一个ms智商140以上的人才能过关的游戏......谁来支招。小明这个人向来就比较无聊, 所以他就玩了起来,但是由于他本人智商的确不怎么样,第二关都没有玩过去T_T,他一气之下,决定写个程序,帮他跑出结果。
游戏是这样的,我们可以控制上图中start点所指向的方块,我们称它为动点,使它向上下左右四个方向运动,一但动点碰到其它的方块,它才会停下来,但是如果没有遇到其它的方块它就会直接飞出屏幕,GAME OVER。如果动点碰到end点,它就会直接起去,而不会飞出去,然后就达到了目的进入下一关。


Input
多组测试数据。
每一组数据第一行为R和C,(R,C<=15)。
以下R行,每行C个字符,表示地图的描述
#表示方块。
*表示空地。
S表示动点初始位置
E表示终点位置。
S和E在地图中只会出现一次。


Output
第i组数据输出前,请先输出“Case I:”.
表示每组输入,请先输出最快到达终点的方案所需要的步数,然后下一行输出所走的方案,u表示向上,d表示向下,l表示向左,r表示向右,数据保证有解并且解唯一。


Sample Input

9 8
***#****
******#*
*#******
*******#
***S#***
E*******
#*******
*****#**
**#*****
9 12
*****#******
*********#**
**##********
#**#*******E
*******S#***
****#*****#*
******#*****
*#*******#**
*******#****


Sample Output

Case 1:
12
urdluruldrul
Case 2:
8
drulurdr


Source
xiaoming
 
 
 
模拟题
 
 
 
#include <iostream>
using namespace std;

typedef struct queue
{
    int x;
    int y;
    int s;
    int di;
    int pre;
}Q, * QPtr;

char sp[4] = {'u', 'd', 'l', 'r'};


int main()
{
    char m[17][17];
    Q q[225];
    int count = 1;
    int r, c;   
    int top = -1, end = 0;
    int flag;
    int xx, yy, ss, d;
    int order[225] = {0}, loc;
    
    while (cin >> r >> c)
    {
        top = -1;
        end = 0;
        
        for (int i = 0; i < r; i++)
            scanf("%s", m[i]);
                
        for (int i = 0; i < r; i++)
            for (int j = 0; j < c; j++)
                if (m[i][j] == 'S')
                {
                    q[0].x = i;
                    q[0].y = j;
                    q[0].s = 0;
                    q[0].di = -1;
                    q[0].pre = -1;
                }        
        flag = 1;
        while (flag)
        {
            top++;
            xx = q[top].x;
            yy = q[top].y;
            ss = q[top].s;           
            d = 0;
            
            while (d < 4)
            {
                switch (d)
                {
                    case 0:
                         for (int i = xx - 1; i >= 0; i--)
                         {
                             if (m[i][yy] == 'E')
                             {
                                 end++;
                                 q[end].x = i;
                                 q[end].y = yy;
                                 q[end].s = ss+1;
                                 q[end].di = d;
                                 q[end].pre = top;
                                 d = 4;
                                 flag = 0;
                                 break;
                             }
                             else if (m[i][yy] == '#')
                             {
                                 if (m[i+1][yy] != 'S')
                                 {
                                     m[i+1][yy] = 'S';//标记已经遍历 
                                     end++;
                                     q[end].x = i+1;
                                     q[end].y = yy;
                                     q[end].s = ss+1;
                                     q[end].di = d;
                                     q[end].pre = top;
                                 }
                                 break;
                             }
                         }
                         break;
                    case 1:
                         for (int i = xx + 1; i < r; i++)
                         {
                             if (m[i][yy] == 'E')
                             {
                                 end++;
                                 q[end].x = i;
                                 q[end].y = yy;
                                 q[end].s = ss+1;
                                 q[end].di = d;
                                 q[end].pre = top;
                                 d = 4;
                                 flag = 0;                                 
                                 break;
                             }
                             else if (m[i][yy] == '#')
                             {
                                 if (m[i-1][yy] != 'S')
                                 {
                                     m[i-1][yy] = 'S';
                                     
                                     end++;
                                     q[end].x = i-1;
                                     q[end].y = yy;
                                     q[end].s = ss+1;
                                     q[end].di = d;
                                     q[end].pre = top;
                                 }
                                 break;
                             }
                         }
                         break;
                    case 2:
                         for (int i = yy - 1; i >= 0; i--)
                         {
                             if (m[xx][i] == 'E')
                             {
                                 end++;
                                 q[end].x = xx;
                                 q[end].y = i;
                                 q[end].s = ss+1;
                                 q[end].di = d;
                                 q[end].pre = top;
                                 d = 4;
                                 flag = 0;
                                 
                                 break;
                             }
                             else if (m[xx][i] == '#')
                             {
                                 if(m[xx][i+1] != 'S')
                                 {
                                     m[xx][i+1] = 'S';
                                     
                                     end++;
                                     q[end].x = xx;
                                     q[end].y = i+1;
                                     q[end].s = ss+1;
                                     q[end].di = d;
                                     q[end].pre = top;
                                 }
                                 break;
                             }
                         }
                         break;
                    case 3:
                         for (int i = yy + 1; i < c; i++)
                         {
                             if (m[xx][i] == 'E')
                             {
                                 end++;
                                 q[end].x = xx;
                                 q[end].y = i;
                                 q[end].s = ss+1;                                 
                                 q[end].di = d;
                                 q[end].pre = top;
                                 d = 4;
                                 flag = 0;
                                 break;
                             }
                             else if (m[xx][i] == '#')
                             {
                                 if(m[xx][i-1] != 'S')
                                 {
                                     m[xx][i-1] = 'S';
                                     
                                     end++;
                                     q[end].x = xx;
                                     q[end].y = i-1;
                                     q[end].s = ss+1;
                                     q[end].di = d;
                                     q[end].pre = top;
                                 }
                                 break;
                             }
                         }
                         break;
                }
                d++;
            }        
        }        
        cout << "Case " << count << ":" << endl;
        count ++;
        
        cout << q[end].s << endl;
        
        loc = q[end].s;
        for (int i = loc; i > 0; i--)
        {
            order[i] = q[end].di;
            end = q[end].pre;
        }
        for (int i = 1; i <= loc; i++)
        {
            printf("%c", sp[order[i]]);
        }        
        cout << endl;                
    } 
}


 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值