poj2632 Crashing Robots 模拟水题
2014-03-09 11:37:46
题意就不解释了,很容易明白,直接上代码
1 ///2014.3.8 2 ///poj2632 3 4 /** 5 *模拟水题,主要是细心 6 *写了好长时间,自己编程速度还是不够 7 *速度不够有几个原因 8 *首先是整体思维的把握不够强烈, 9 * 有写到后面改前面的情况 10 *其次是编码不够熟练,积累不够 11 */ 12 13 #include <iostream> 14 #include <cstdio> 15 #include <cstring> 16 using namespace std; 17 18 struct robot{ 19 int x,y; ///位置 20 int direction; ///方向,0表示向东,1表示向北, 21 /// 2表示向西,3表示向南。 22 }; 23 24 struct instr{ ///指令,instructions 25 int num; ///机器人编号 26 char act; ///行为 27 int step; ///执行行为的次数 28 }; 29 30 int main( ) 31 { 32 // freopen("in","r",stdin); 33 // freopen("out","w",stdout); 34 35 int K; ///case数目 36 scanf("%d",&K); 37 while( K-- ){ 38 int A,B; ///机器人活动区域大小,A表示长,B表示宽 39 scanf("%d%d",&A,&B); 40 int N,M; ///N是机器人数目,M是指令数目 41 scanf("%d%d",&N,&M); 42 43 int area[A+1][B+1]; ///机器人的活动区域 44 ///每一个点的数据是该点的无机器人号 45 ///0表示该点没有机器人 46 memset(&area[0][0],0,sizeof(int)*(A+1)*(B+1) ); ///初始化,全设为0(没有机器人) 47 robot rob[N+1]; 48 for(int i=1 ; i<=N ; i++){ ///读入机器人位置和方向 49 cin>>rob[i].x>>rob[i].y; 50 area[rob[i].x][rob[i].y] = i; 51 char direction; 52 cin>>direction; 53 switch( direction ){ 54 case 'E': 55 rob[i].direction = 0; break; 56 case 'N': 57 rob[i].direction = 1; break; 58 case 'W': 59 rob[i].direction = 2; break; 60 case 'S': 61 rob[i].direction = 3; break; 62 } 63 } 64 65 bool OK = true; 66 int robotNum,carshNum; 67 for(int m=0 ; m<M ; m++){ 68 instr ins; 69 cin>>ins.num>>ins.act>>ins.step; 70 if( OK ){ 71 switch( ins.act ){ 72 case 'L': 73 rob[ins.num].direction = (rob[ins.num].direction+1*ins.step)%4; 74 break; 75 case 'R': 76 rob[ins.num].direction = (rob[ins.num].direction+3*ins.step)%4; 77 break; 78 case 'F': 79 for(int j=0 ; j<ins.step ; j++){ 80 if( !OK ) break; 81 int next_x,next_y; 82 switch( rob[ins.num].direction ){ 83 case 0: 84 { 85 next_x = rob[ins.num].x + 1; 86 next_y = rob[ins.num].y; 87 if( next_x>A ){ 88 robotNum = ins.num; 89 carshNum = -1; ///撞到-1号机器人表示撞墙 90 OK = false; 91 } 92 else if( area[next_x][next_y] ){ 93 robotNum = ins.num; 94 carshNum = area[next_x][next_y]; 95 OK = false; 96 } 97 else{ 98 area[next_x][next_y] = ins.num; 99 area[rob[ins.num].x][rob[ins.num].y] = 0; 100 rob[ins.num].x = next_x; 101 rob[ins.num].y = next_y; 102 } 103 } break; 104 case 1: 105 { 106 next_x = rob[ins.num].x; 107 next_y = rob[ins.num].y+1; 108 if( next_y>B ){ 109 robotNum = ins.num; 110 carshNum = -1; ///撞到-1号机器人表示撞墙 111 OK = false; 112 } 113 else if( area[next_x][next_y] ){ 114 robotNum = ins.num; 115 carshNum = area[next_x][next_y]; 116 OK = false; 117 } 118 else{ 119 area[next_x][next_y] = ins.num; 120 area[rob[ins.num].x][rob[ins.num].y] = 0; 121 rob[ins.num].x = next_x; 122 rob[ins.num].y = next_y; 123 } 124 } break; 125 case 2: 126 { 127 next_x = rob[ins.num].x-1; 128 next_y = rob[ins.num].y; 129 if( next_x<1 ){ 130 robotNum = ins.num; 131 carshNum = -1; ///撞到-1号机器人表示撞墙 132 OK = false; 133 } 134 else if( area[next_x][next_y] ){ 135 robotNum = ins.num; 136 carshNum = area[next_x][next_y]; 137 OK = false; 138 } 139 else{ 140 area[next_x][next_y] = ins.num; 141 area[rob[ins.num].x][rob[ins.num].y] = 0; 142 rob[ins.num].x = next_x; 143 rob[ins.num].y = next_y; 144 } 145 } break; 146 case 3: 147 { 148 next_x = rob[ins.num].x; 149 next_y = rob[ins.num].y-1; 150 if( next_y<1 ){ 151 robotNum = ins.num; 152 carshNum = -1; ///撞到-1号机器人表示撞墙 153 OK = false; 154 } 155 else if( area[next_x][next_y] ){ 156 robotNum = ins.num; 157 carshNum = area[next_x][next_y]; 158 OK = false; 159 } 160 else{ 161 area[next_x][next_y] = ins.num; 162 area[rob[ins.num].x][rob[ins.num].y] = 0; 163 rob[ins.num].x = next_x; 164 rob[ins.num].y = next_y; 165 } 166 } break; 167 } 168 } 169 } 170 } 171 } 172 173 if( OK ) 174 cout<<"OK"<<endl; 175 else{ 176 if( carshNum==-1 ) 177 printf("Robot %d crashes into the wall\n",robotNum); 178 else 179 printf("Robot %d crashes into robot %d\n",robotNum,carshNum); 180 } 181 } 182 return 0; 183 }