Sicily 9161. VOYAGER

9161. VOYAGER

Constraints

Time Limit: 1 secs, Memory Limit: 256 MB

Description

The Voyager 1 space probe (not to be confused with the Intrepid-class starship) was launched a long
time ago, in 1977, and is currently on the verge of leaving our Solar System. As it travels further
through space, it has been programmed to leave a radio signal message in any star system it stumbles
upon, to mark the probe's path for as long as possible.
Let us assume that a star system can be represented by a rectangular grid with N rows and M
columns, dividing the space into N by M equal cells. Each cell can contain a single planet, black
hole, or be empty. The probe broadcasts the signal from a pre-determined empty cell, in one of the
four axis-aligned directions (U?up, R?right, D?down, L?left).


Upon being broadcast, the signal propagates in a straight line along the same row/column until it
reaches a planet, where it is deflected by 90 degrees in another direction. There are two kinds of
planets, which we will denote by / and \ The deflection rules are shown in the image below:
The signal permanently leaves the system upon either entring a cell containing a black hole, or
propagating outside the edges of the rectangular grid. It is also known that the signal needs one second
to propagate from the current cell to a neighbouring one.

 


Write a program to determine the direction in which the probe needs to broadcast the signal so that it
remains within the system for as long as possible, outputting the optimal direction as well as the
resulting longest time. If it is possible for the signal to remain in the system indefinitely, output the
message Voyager instead of the required time.

Input

The first line of input contains two positive integers, N (1 ≤ N ≤ 500) and M (1 ≤ M ≤ 500).
Each of the following N lines contains M characters from the set {“/”, “\”, “C”, “.”}, where “/” and
“\” represent the two kinds of planets, “C” represents a black hole, and “.” represents an empty cell.
The last line of input contains two positive integers, PR (1 ≤ PR ≤ N) and PC (1 ≤ PC ≤ M), the row
and column number, respectively, of the cell where the probe is situated.

Output

The first line of output must contain the required optimal broadcast direction (“U”, “R”, “D”, or “L”).
If the solution is not unique, select the first optimal one in the following priority order: first “U”, then
“R”, then “D”, and finally “L”.
The second line of output must contain the required longest time (or message).

Sample Input

5 5
../.\
.....
.C...
...C.
\.../
3 3

Sample Output

U
17

Problem Source

2013年每周一赛第10场/COCI 2013.1

没做什么优化,比较繁琐的模拟题:0.01s:

#include <stdio.h>
#include <string.h>

int used[505][505];//用于记录是否走过,同一个点走过3次或以上就是死循环
char mapp[505][505];
char dir[5] = "URDL";
int max = 0;//最大步数
char max_dir = 'U';//最大步数的方向
int sp_i, sp_j, h, w;
bool indefinition = false;//是否是死循环    
char now_dir = 'U';//现在的起始方向


struct step {
    char dir;
    int ii;
    int jj;
    int num;
    step(char d, int i, int j, int n) {
        dir = d;
        ii = i;
        jj = j;
        num = n;
    }
};

bool is_valid(int i, int j) {
    if (i < 0 || j < 0 || i >= h || j >= w || mapp[i][j] == 'C') {
        return false;
    }
    return true;
}

void gogogo(int i) {
    memset(used, 0, sizeof(used));
    step go(dir[i], sp_i - 1, sp_j - 1, 0);
    now_dir = go.dir;
    for (int j = 0; j < 4; j++) {
        if (go.dir == dir[j]) {
            used[go.ii][go.jj] = j + 1;
        }
    }
    while (1) {
        if (go.dir == 'U') {
            go.ii--;
        } else if (go.dir == 'R') {
            go.jj++;
        } else if (go.dir == 'D') {
            go.ii++;
        } else {
            go.jj--;
        }
        go.num++;
        
        if (!is_valid(go.ii, go.jj)) {
            break;
        }
        
        if (used[go.ii][go.jj] > 2) {//判断死循环
            printf("%c\n", now_dir);
            indefinition = true;
            break;
        }
        
        used[go.ii][go.jj]++;
        
        if (mapp[go.ii][go.jj] == '\\') {//方向的改变
            if (go.dir == 'U') {
                go.dir = 'L';
            } else if (go.dir == 'R') {
                go.dir = 'D';
            } else if (go.dir == 'D') {
                go.dir = 'R';
            } else {
                go.dir = 'U';
            }
        } else if (mapp[go.ii][go.jj] == '/') {
            if (go.dir == 'U') {
                go.dir = 'R';
            } else if (go.dir == 'R') {
                go.dir = 'U';
            } else if (go.dir == 'D') {
                go.dir = 'L';
            } else {
                go.dir = 'D';
            }
        }
    }
    if (go.num > max) {//更新最大值以及方向
        max = go.num;
        max_dir = now_dir;
    }
}
    
int main() {
    scanf("%d %d\n", &h, &w);
    for (int i = 0; i < h; i++) {
        gets(mapp[i]);
    }
    scanf("%d %d", &sp_i, &sp_j);
    for (int i = 0; i < 4; i++) {
        gogogo(i);
        if (indefinition) {
            printf("Voyager\n");
            return 0;
        }
    }
    printf("%c\n%d\n", max_dir, max);
    return 0;
}                     

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值