您的位置:首页 > 其它

Codeforces 8B Obsession With Robot 简单图遍历

2015-07-02 09:04 405 查看
题目链接:
http://codeforces.com/problemset/problem/8/B
题意:

问从起点到终点的路径是否为最短路径。假设每个格子不是障碍就是空格。

思路:

具体实现开一个较大的vis数组、然后从中心开始行走即可,关键是题意理解。空格是障碍,说明俩空格之间可达是看空格值而不是空格相接的边的的值,因此推出只要达到的当前空格有相邻空格访问过、并且相邻空格不是之前来的那个空格,就说明该路径不是最短路径。

然而初始化处理起点为设置为访问过又W一发。

源码:

#include <cstdio>

#include <cmath>

#include <cstring>

#include <algorithm>

#include <iostream>

#include <string>

using namespace std;

const int MAXN = 500;

int vis[MAXN][MAXN];

char data[MAXN];

int val(char c)

{

if(c == 'U')

return 0;

else if(c == 'R') return 1;

else if(c == 'D') return 2;

else if(c == 'L') return 3;

}

int dx[] = {-1,0,1,0};

int dy[] = {0,1,0,-1};

int main()

{

while(scanf("%s",data) != EOF){

memset(vis, 0, sizeof(vis));

int x,y;

int prex,prey;

prex = prey = 200;

vis[prex][prey] = 1;

x = prex + dx[val(data[0])];

y = prey + dy[val(data[0])];

prex = x;

prey = y;

int flag = 1;

for(int i=1; i<strlen(data); i++){

vis[x][y] = 1;

int mark = val(data[i]);

x += dx[mark];

y += dy[mark];

if(vis[x][y] == 1){

flag = 0;

break;

}

for(int j=0; j<4; j++){

int tx = x + dx[j];

int ty = y + dy[j];

if((tx != prex || ty != prey) && vis[tx][ty] == 1){

// printf("prex = %d, prey = %d\n",prex,prey);

// printf("X = %d,y = %d,tx = %d,ty = %d\n",x,y,tx,ty);

flag = 0;

break;

}

}

prex = x;

prey = y;

}

if(flag == 1)

printf("OK\n");

else

printf("BUG\n");

}

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: