题意:
年轻的拉尔夫开玩笑地从一个小镇上偷走了一辆车,但他没想到的是那辆车属于警察局,并且车上装有用于发射车子移动路线的装置。
那个装置太旧了,以至于只能发射关于那辆车的移动路线的方向信息。
编写程序,通过使用一张小镇的地图帮助警察局找到那辆车。程序必须能表示出该车最终所有可能的位置。
小镇的地图是矩形的,上面的符号用来标明哪儿可以行车哪儿不行。“.”表示小镇上那块地方是可以行车的,而符号“X”表示此处不能行车。拉尔夫所开小车的初始位置用字符的“*”表示,且汽车能从初始位置通过。
汽车能向四个方向移动:向北(向上),向南(向下),向西(向左),向东(向右)。
拉尔夫所开小车的行动路线是通过一组给定的方向来描述的。在每个给定的方向,拉尔夫驾驶小车通过小镇上一个或更多的可行车地点。
思路:
有点模拟的意思吧。。。
就是当读到一个方向的时候,就把原地图上所有有小车的点向该方向平移,平移小车会在地图上形成一条轨迹,轨迹上的每一个点都会计入下一次的平移
然后就好了
代码:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std; 6 #define MAXN 110 7 const int dy[5]={0,-1,0,1,0}; 8 const int dx[5]={0,0,-1,0,1}; 9 int mmap[MAXN][MAXN],tmpmap[MAXN][MAXN]; 10 int i,j,k,m,n,r,p,f,x,y; 11 char str[MAXN]; 12 char c; 13 int main(){ 14 #ifndef ONLINE_JUDGE 15 freopen("search.in","r",stdin); 16 freopen("search.out","w",stdout); 17 #endif 18 //scanf("%d%d",&n,&m); 19 //getchar(); //此处输入方式与下方不同 20 cin>>n>>m; 21 memset(mmap,0,sizeof(mmap)); 22 for(i=1;i<=n;i++){ 23 for(j=1;j<=m;j++){ 24 //c=getchar(); //此处输入方式与下方不同 25 cin>>c; 26 if(c==‘.‘){ 27 mmap[i][j]=1; 28 }else{ 29 if(c==‘*‘) mmap[i][j]=2; 30 } 31 } 32 getchar(); 33 } 34 scanf("%d",&p); 35 getchar(); 36 for(r=1;r<=p;r++){ 37 scanf("%s",str+1); 38 //gets(str+1); 39 if(str[1]==‘N‘){ 40 f=1; 41 }else{ 42 if(str[1]==‘W‘){ 43 f=2; 44 }else{ 45 if(str[1]==‘S‘){ 46 f=3; 47 }else{ 48 f=4; 49 } 50 } 51 } 52 for(i=1;i<=n;i++){ 53 for(j=1;j<=m;j++){ 54 if(mmap[i][j]==2){ 55 x=j+dx[f],y=i+dy[f]; 56 while(x>0&&x<=m&&y>0&&y<=n&&mmap[y][x]){ 57 tmpmap[y][x]=2; 58 x+=dx[f],y+=dy[f]; 59 } 60 } 61 } 62 } 63 for(i=1;i<=n;i++){ 64 for(j=1;j<=m;j++){ 65 if(mmap[i][j]==2) mmap[i][j]=1; 66 if(tmpmap[i][j]==2) mmap[i][j]=2,tmpmap[i][j]=0; 67 } 68 } 69 } 70 for(i=1;i<=n;i++){ 71 for(j=1;j<=m;j++){ 72 if(mmap[i][j]==0){ 73 printf("%c",‘X‘); 74 }else{ 75 if(mmap[i][j]==1) printf("%c",‘.‘); else printf("%c",‘*‘); 76 } 77 } 78 putchar(10); 79 } 80 return 0; 81 }
原文:https://www.cnblogs.com/linxif2008/p/9691721.html