DLU-1033 找到你了!M-I-K-U!
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <malloc.h> 4 #include <string.h> 5 #include <stdbool.h> 6 #include <math.h> 7 8 9 bool FindYourMiku(char **board,char *MIKU,int cur_x,int cur_y,int sizeRow,int sizeCol,int MikuStart) 10 { 11 if(MikuStart == 4) 12 return true; 13 else if(cur_x<0 || cur_x>=sizeRow || cur_y<0 || cur_y>=sizeCol 14 || board[cur_x][cur_y] != MIKU[MikuStart]) 15 return false; 16 17 board[cur_x][cur_y] ^= 255;//mark to avoid endless loop 18 bool result = ( FindYourMiku(board,MIKU,cur_x-1,cur_y,sizeRow,sizeCol,MikuStart+1) 19 || FindYourMiku(board,MIKU,cur_x,cur_y-1,sizeRow,sizeCol,MikuStart+1) 20 || FindYourMiku(board,MIKU,cur_x+1,cur_y,sizeRow,sizeCol,MikuStart+1) 21 || FindYourMiku(board,MIKU,cur_x,cur_y+1,sizeRow,sizeCol,MikuStart+1) ); 22 board[cur_x][cur_y] ^= 255; 23 return result; 24 } 25 26 bool exist(char **board,char *MIKU,int sizeRow,int sizeCol) 27 { 28 if(sizeRow<=0) 29 return false; 30 31 int i,j; 32 for(i = 0;i < sizeRow;i ++) 33 { 34 for(j = 0;j < sizeCol;j ++) 35 { 36 if(board[i][j] == 'M') 37 { 38 bool isFind = FindYourMiku(board,MIKU,i,j,sizeRow,sizeCol,0); 39 //printf("GOOD\n"); 40 if(isFind==true) 41 return true; 42 } 43 } 44 } 45 return false; 46 } 47 48 int main() 49 { 50 int m,n; 51 int i,j; 52 while(~scanf("%d %d",&m,&n)) 53 { 54 char **board = (char**)malloc(m*sizeof(char*)); 55 for(i = 0;i < m;i ++) 56 { 57 board[i] = (char*)malloc(n*sizeof(char)); 58 } 59 getchar(); 60 for(i = 0;i < m;i ++) 61 { 62 for(j = 0;j < n;j ++) 63 { 64 scanf("%c",&board[i][j]); 65 getchar(); 66 } 67 } 68 char MIKU[4] = {'M','I','K','U'}; 69 bool result = exist(board,MIKU,m,n); 70 if(result==true) 71 printf("mitsuketa!\n"); 72 else 73 printf("zannen\n"); 74 } 75 return 0; 76 }