Cat and Mouse Blue Bridge cup/hand speed/Violence Practice Contest "The cat and the mouse move in the squares of the 10*10, for example: *...* ..... *...*. .......... ...*. C ..... *.....* ......... m......* .... * * * ..... *.*...... C= Cat (cat) m= Mouse (MOUSE) *= obstacle. = Open space cats and mice walk one block per second, and if they are in the same lattice at the end of a second, we call them "meet." Note that "to wear" is not a meeting. The cat and mouse move the same way: usually along a straight line, the next step if it will go up or out of bounds, with 1 seconds to do a right turn 90 degrees. At first they were all facing the north. Programmed to calculate how many seconds later they meet. "Input format" 10 lines, format as above "output format" Meet time t. If there is no solution, output-1. "Sample Input" *...* ..... *...* .......... .......... ...*. C ..... *.....* ......... m......* .... * * * ..... *.*......
1 /*2 idea: If the cat and the mouse do not meet, then it must be the cat in a certain lattice along a certain direction repeatedly walked more than 2 times and3 so is the mouse. 4 */ 5#include <iostream>6#include <cstring>7#include <cstdio>8#include <algorithm>9 using namespacestd;Ten One Charmp[ the][ the]; A - Const intLeft_dir =1; - Const intRight_dir =2; the Const intup =3; - Const intDown =4; - Const intMouse =1; - Const intCat =0; + - structnode{ + intx, y; A intdir; at node () { -DIR =Up ; - } - - BOOLflag[ the][ the][5];//record whether the current grid has passed in a certain direction - in voidInitintXinty) { -memset (Flag,false,sizeof(flag)); to This->x =x; + This->y =y; -Flag[x][y][up] =true; the } * }; $ Panax NotoginsengNode nd[2]; - intCNT =0; the + BOOLMoveinti) { A intx=nd[i].x, y=nd[i].y; the intNewdir; + Switch(nd[i].dir) { - CaseUp : $x-=1; $Newdir =Right_dir; - Break; - CaseDown : thex+=1; -Newdir =Left_dir;Wuyi Break; the CaseLeft_dir: -y-=1; WuNewdir =Up ; - Break; About CaseRight_dir: $y+=1; -Newdir =Down ; - Break; - } A if(Mp[x][y]! ='*'){ +nd[i].x =x; theND[I].Y =y; -}Else { $Nd[i].dir =Newdir; the } the the if(!Nd[i].flag[x][y][nd[i].dir]) { theNd[i].flag[x][y][nd[i].dir] =true; - return true; in}Else return false; the } the About voidTest () { the BOOLFlag =true, flag1=false, flag2=false; the while(flag) { the if(nd[cat].x = = nd[mouse].x && nd[cat].y = = nd[mouse].y) Break; + if(!Move (cat)) -Flag1 =true; the if(!Move (mouse))BayiFlag2 =true; the++CNT; the if(Flag1 && flag2) flag =false; - } - if(flag) printf ("%d\n", CNT); the Elseprintf"-1\n"); the } the the intMain () { -Memset (MP,'*',sizeof(MP)); the for(intI=1; i<=Ten; ++i) { thescanf"%s", mp[i]+1); theMp[i][strlen (mp[i]+1)+1] ='*';94 for(intj=1; j<=Ten; ++j) { the if(mp[i][j]=='C'){ the Nd[cat].init (i, j); theMP[I][J] ='.';98 } About Else if(Mp[i][j] = ='M'){ - Nd[mouse].init (i, j);101MP[I][J] ='.';102 }103 }104 GetChar (); the }106 test ();107 return 0;108 }109 /* the *...*.....111 ......*... the ...*...*..113 .......... the ...*. C .... the *.....*... the ...*......117 .. m......*118 ...*.*....119 .*.*...... - */
Cat and Mouse Blue Bridge cup/hand speed/violence practice (violent search)