CodeVs 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.
時間限制: 1 s  空間限制: 128000 KB


解體思路

比較暴力的BFS,注意判重,不然會T


#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <numeric>
#include <functional>
using namespace std;
#define MAX 55
typedef struct da{
	char now;
	int sept;
	int a,b;
}da;
int N,R,C;
char maps[MAX][MAX];
int vis[MAX][MAX],number[MAX][MAX];
queue <da>p;
void bfs(int a,int b){
	int set=0,flag=0;
	char dir[6];
	memset(dir,0,sizeof(dir));
	da temp,next,before;
	temp.a=a;
	temp.b=b;
	temp.now=maps[a][b];
	temp.sept=0;
	before.sept=0;
	p.push(temp);
	while(!p.empty()){
		temp=p.front();
		p.pop();
		vis[temp.a][temp.b]=0;
		//flag=0;
		if(temp.sept<N){
			if(set==0||temp.sept>before.sept){
				cin>>dir;
			}
			set++;
			if(strcmp(dir,"NORTH")==0){
				for(next.a=temp.a-1,next.b=temp.b;next.a>=0;next.a--){
					if(maps[next.a][next.b]=='X'){
						break;
					}
					next.now=maps[next.a][next.b];
					next.sept=temp.sept+1;
					if(vis[next.a][next.b]==0||number[next.a][next.b]!=next.sept){
					//	flag=1;
						vis[next.a][next.b]=1;
						number[next.a][next.b]=next.sept;
						p.push(next);
					}
					else{
						break;
					}
				}
			}
			if(strcmp(dir,"SOUTH")==0){
				for(next.a=temp.a+1,next.b=temp.b;next.a<R;next.a++){
					if(maps[next.a][next.b]=='X'){
						break;
					}
					next.now=maps[next.a][next.b];
					next.sept=temp.sept+1;
					if(vis[next.a][next.b]==0||number[next.a][next.b]!=next.sept){
					//	flag=1;
						vis[next.a][next.b]=1;
						number[next.a][next.b]=next.sept;
						p.push(next);
					}
					else{
						break;
					}
				}
			}
			if(strcmp(dir,"WEST")==0){
				for(next.a=temp.a,next.b=temp.b-1;next.b>=0;next.b--){
					if(maps[next.a][next.b]=='X'){
						break;
					}
					next.now=maps[next.a][next.b];
					next.sept=temp.sept+1;
					if(vis[next.a][next.b]==0||number[next.a][next.b]!=next.sept){
					//	flag=1;
						vis[next.a][next.b]=1;
						number[next.a][next.b]=next.sept;
						p.push(next);
					}
					else{
						break;
					}
				}
			}
			if(strcmp(dir,"EAST")==0){
				for(next.a=temp.a,next.b=temp.b+1;next.b<C;next.b++){
					if(maps[next.a][next.b]=='X'){
						break;
					}
					next.now=maps[next.a][next.b];
					next.sept=temp.sept+1;
					if(vis[next.a][next.b]==0||number[next.a][next.b]!=next.sept){
					//	flag=1;
						vis[next.a][next.b]=1;
						number[next.a][next.b]=next.sept;
						p.push(next);
					}
					else{
						break;
					}
				}
			}
		}
		if(temp.sept==N){
			maps[temp.a][temp.b]='*';
		}
		before=temp;
	}
}
void print(){
	int i,j;
	for(i=0;i<R;i++){
		for(j=0;j<C;j++)
			printf("%c",maps[i][j]);
		printf("\n");
	}
}
int main(void){
	int i,j,a,b;
	while(cin>>R>>C){
		memset(maps,0,sizeof(maps));
		memset(vis,0,sizeof(vis));
		memset(number,0,sizeof(number));
		for(i=0;i<R;i++){
			for(j=0;j<C;j++){
				cin>>maps[i][j];
				if(maps[i][j]=='*'){
					a=i;
					b=j;
					maps[i][j]='.';
				}
			}
		}
		cin>>N;
		bfs(a,b);
		print();
	}
	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章