wikioi逃跑的拉尔夫c++

#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 );
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值