SDUT OJ 2718

首先说一下:写这道题的目的主要是为了让自己记住,敲代码的时候一定得细心啊!因为今天比赛的时候我交死活都不对、、、很无奈啊,本来很简单的题目写了好长时间啊、、、比赛结束之后做还是不对。最终我才发现是有两个变量写错了啊!改了就AC了啊、、这个很值得反思啊,写代码的时候一定要小心啊!!!

题意是:一个人从边界进入一个矩阵中,(不从四个角进入),然后从某一个边上出来,让你求出来的那个点的位置,以及一共走过了多少个格。有岩石的地方是不可以走的;每次直着向前走,遇到岩石或者是走出去了才停下来;如果前面有岩石先向右走,如果右面也有岩石,那就向左边走,如果左边也有岩石那就直接原路返回。


就是一个搜索,不解释了啊、、


Rocky

Time Limit: 1000MS Memory limit: 65536K

题目描述

Sylvester Stallion is an old horse who likes nothing better than to wander around in the fields around his stable. Sylvester is a little single minded and will walk in a straight line unless there is a rock in this path. If that\'s the case, he does one of three things: 1) if there is no rock to his right, he turns right and continues walking straight in that direction; 2) otherwise, if there is no rock to his left, he turns left and walks in that direction; 3) otherwise, he turns around and walks back the way he came. In a particularly rocky field, he may make several turns in his walk and exit the field in quite an unexpected location. For example, in the field shown below, if Sylvester enters the field at square (1,4), he will follow the path shown (a total of 12 squares), exiting at square (3,5).

Many of his other animal friends are concerned about Sylvester, and would like to know where he ends up on his walks (in particular, his good friend, the ram Beau). That\'s where you come in - given a description of a field and the location of Sylvester\'s entrance, you are to determine where he will exit the field and how long it will take him to get there.

输入

 Each case starts with three positive integers n m rindicating the number of columns (n) and rows (m) in the field and the number of rocks (r), with n, m  20. Following the first line will be lines containing the locations of the r rocks. Each location will be of the form c r, indicating the column and row of the rock. There will be no more than 1 rock in any location. Following the rock locations will be the entrance location for Sylvester. Sylvester\'s starting direction will always be perpendicular to the side of the field he enters from (this location will never be a corner square) and there will never be a rock in his entrance location square. A line containing three zeros will terminate input.

输出

 For each test case, output the case number followed by the last square Sylvester hits before he leaves the field (Sylvester will never get trapped in any field) and the number of squares that Sylvester visited during his walks, counting repeated squares.

示例输入

6  5  7
2  2  2  5  3  1  4  4
5  1  5  3  6  2
1  4
5  5  0
1  2
0  0  0

示例输出

Case 1: 3 5 12
Case 2: 5 2 5

提示

 

来源

中国海洋大学第四届朗讯杯高级组
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#define M 10010
#define INF 1 << 30;


using namespace std;

int map1[100][100];

int main()
{
    //freopen("a.txt","w",stdout);
    int n, m, k;
    int cnt = 0, step;
    int d;
    while(cin >>n>>m>>k)
    {
        if(!n && !m && !k)
            break;
        memset(map1 , 0 , sizeof(map1));
        int x1, y1;
        for(int i = 0; i < k; i++)
        {
            cin >>x1>>y1;
            map1[x1][y1] = 1;
        }
        //cout <<map1[2][5]<<endl;
        int xx, yy;
        cin >>xx>>yy;

        step = 1;

        if(xx == 1)
            d = 4;
        if(xx == n)
            d = 2;
        if(yy == 1)
            d = 1;
        if(yy == m)
            d = 3;
        while(1)
        {
            if(d == 1)//up
            {
                int i;
                for(i = yy+1; i <= m; i++)
                {
                    if(map1[xx][i])
                    {
                        break;
                    }
                    else
                        step++;
                }
                i -= 1;
                if(i == m)
                {
                    yy = m;
                    break;
                }
                else
                {
                    yy = i;
                    if(!map1[xx+1][yy])//right;
                        d = 4;
                    else
                    {
                        if(!map1[xx-1][yy])//left;
                            d = 2;
                        else
                            d = 3;//return;
                    }
                }
            }
            if(d == 2)//left;
            {
                int i;
                for(i = xx-1; i >= 1; i--)
                {
                    if(map1[i][yy])
                    {
                        break;
                    }
                    else
                        step ++;
                }
                i += 1;
                if(i == 1)
                {
                    xx = 1;
                    break;
                }
                else
                {
                    xx = i;
                    if(!map1[xx][yy+1])//right ;
                        d = 1;
                    else
                    {
                        if(!map1[xx][yy-1])// left;
                            d = 3;
                        else
                            d = 4; //return;
                    }
                }
            }
            if(d == 3)//down
            {
                int i;
                for(i = yy-1; i >= 1; i--)
                {
                    if(map1[xx][i])
                    {
                        break;
                    }
                    else
                        step++;
                }
                i += 1;
                if(i == 1)
                {
                    yy = 1;
                    break;
                }
                else
                {
                    yy = i;
                    if(!map1[xx-1][yy])//right;
                        d = 2;
                    else
                    {
                        if(!map1[xx+1][yy])//left;
                            d = 4;
                        else
                            d = 1;//return ;
                    }
                }
            }
            if(d == 4)//right
            {
                int i;
                for(i = xx+1; i <= n; i++)
                {
                    if(map1[i][yy])
                    {
                        break;
                    }
                    else
                        step++;
                }
                i -= 1;
                if(i == n)
                {
                    xx = n;
                    break;
                }
                else
                {
                    xx = i;
                    if(!map1[xx][yy-1])//right;
                        d = 3;
                    else
                    {
                        if(!map1[xx][yy+1])//left;
                            d = 1;
                        else
                            d = 2;//return;
                    }
                }
            }
        }
        cout <<"Case "<<++cnt<<": "<<xx<<' '<<yy<<' '<<step<<endl;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值