题目地址:
http://wikioi.com/problem/1026/
年轻的拉尔夫开玩笑地从一个小镇上偷走了一辆车,但他没想到的是那辆车属于警察局,并且车上装有用于发射车子移动路线的装置。
那个装置太旧了,以至于只能发射关于那辆车的移动路线的方向信息。
编写程序,通过使用一张小镇的地图帮助警察局找到那辆车。程序必须能表示出该车最终所有可能的位置。
小镇的地图是矩形的,上面的符号用来标明哪儿可以行车哪儿不行。“.”表示小镇上那块地方是可以行车的,而符号“X”表示此处不能行车。拉尔夫所开小车的初始位置用字符的“*”表示,且汽车能从初始位置通过。
汽车能向四个方向移动:向北(向上),向南(向下),向西(向左),向东(向右)。
拉尔夫所开小车的行动路线是通过一组给定的方向来描述的。在每个给定的方向,拉尔夫驾驶小车通过小镇上一个或更多的可行车地点。
输入文件的第一行包含两个用空格隔开的自然数R和C,1≤R≤50,1≤C≤50,分别表示小镇地图中的行数和列数。
以下的R行中每行都包含一组C个符号(“.”或“X”或“*”)用来描述地图上相应的部位。
接下来的第R+2行包含一个自然数N,1≤N≤1000,表示一组方向的长度。
接下来的N行幅行包含下述单词中的任一个:NORTH(北)、SOUTH(南)、WEST(西)和EAST(东),表示汽车移动的方向,任何两个连续的方向都不相同。
输出文件应包含用R行表示的小镇的地图(象输入文件中一样),字符“*”应该仅用来表示汽车最终可能出现的位置。
4 5
.....
.X...
...*X
X.X..
3
NORTH
WEST
SOUTH
.....
*X*..
*.*.X
X.X..
其次,深度优先搜索是可以的,不过我用了广度优先搜索。
要注意的就是,用三位数组判重,只用二维会死的很惨。
借助queue实现。
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
int m[ 60 ][ 60 ]; //地图,1表示能通过,0表示不能通过
char newmap[ 60 ][ 60 ]; //新地图
int d[ 1010 ]; //记录方向
int v[ 60 ][ 60 ][ 1010 ]; //是否查看过
struct Node{
int x;
int y;
int n;
void write( int x1, int y1, int n1 ){
x = x1;
y = y1;
n = n1;
}
};
void BFS( int row, int col, int n, Node & node ){
queue< Node > q;
if( !q.empty( ) )
q.pop( );
q.push( node );
v[ node.y ][ node.x ][ node.n ] = 1;
while( !q.empty( ) ){
node = q.front( );
q.pop( );
if( node.n == n ){
newmap[ node.y ][ node.x ] = ‘*‘;
continue;
}
if( d[ node.n ] == 1 ){
for( int i = 1; i < 100; i++ ){
if( m[ node.y ][ node.x - i ] == 0 || node.x - i < 0 )
break;
if( v[ node.y ][ node.x - i ][ node.n + 1 ] == 1 ) //注意是node.n+1, 而不是 node.n
continue;
else {
v[ node.y ][ node.x - i ][ node.n + 1 ] = 1;
Node next;
next.write( node.x - i, node.y, node.n + 1 );
q.push( next );
}
}
}
else if( d[ node.n ] == 2 ){
for( int i = 1; i < 100; i++ ){
if( m[ node.y ][ node.x + i ] == 0 || node.x + i >= col )
break;
if( v[ node.y ][ node.x + i ][ node.n + 1 ] == 1 )
continue;
else {
v[ node.y ][ node.x + i ][ node.n + 1 ] = 1;
Node next;
next.write( node.x + i, node.y, node.n + 1 );
q.push( next );
}
}
}
else if( d[ node.n ] == 3 ){
for( int i = 1; i < 100; i++ ){
if( m [ node.y - i ][ node.x ] == 0 || node.y - i < 0 )
break;
if( v[ node.y - i ][ node.x ][ node.n + 1 ] == 1 )
continue;
else {
v[ node.y - i ][ node.x ][ node.n + 1 ] = 1;
Node next;
next.write( node.x, node.y - i, node.n + 1 );
q.push( next );
}
}
}
else if( d[ node.n ] == 4 ){
for( int i = 1; i < 100; i++ ){
if( m[ node.y + i ][ node.x ] == 0 || node.y + i >= row )
break;
if( v[ node.y + i ][ node.x ][ node.n + 1 ] == 1 )
continue;
else {
v[ node.y + i ][ node.x ][ node.n + 1 ] = 1;
Node next;
next.write( node.x, node.y + i, node.n + 1 );
q.push( next );
}
}
}
}
}
void printMap( int row, int col ){
for( int i = 0; i < row; i++ ){
for( int j = 0; j < col; j++ ){
if( newmap[ i ][ j ] == ‘*‘ )
cout << ‘*‘;
else if( m[ i ][ j ] == 0 )
cout << ‘X‘;
else if( m[ i ][ j ] == 1 )
cout << ‘.‘;
}
cout << endl;
}
}
int main( ){
int row, col;
cin >> row >> col;
Node node;
char c;
for( int i = 0; i < row; i++ ){ //i 表示 y轴
for( int j = 0; j < col; j++ ){ //j 表示 x轴
cin >> c;
if( c == ‘*‘ ){
m[ i ][ j ] = 1;
node.write( j, i, 0 );
}
if( c == ‘.‘ )
m[ i ][ j ] = 1;
if( c == ‘X‘ )
m[ i ][ j ] = 0;
}
}
int n;
cin >> n;
char w[ 10 ];
for( int i = 0; i < n; i++ ){
cin >> w;
if( strcmp( w, "WEST" ) == 0 )
d[ i ] = 1;
else if( strcmp( w, "EAST" ) == 0 )
d[ i ] = 2;
else if( strcmp( w, "NORTH" ) == 0 )
d[ i ] = 3;
else if( strcmp( w, "SOUTH" ) == 0 )
d[ i ] = 4;
}
BFS( row, col, n, node );
printMap( row, col );
}
原文:http://blog.csdn.net/xhy4930633490/article/details/20720279