FOJ 1019 猫捉老鼠

郁闷的半死!

一个输入问题,导致了一直的WA!!!

原来输入是用

if(scanf("%c", c), c=='/n')

   scanf("%c", c);

这样在处理多组数据的时候会出现问题。

后来改为

while(scanf("%c", &c), c == '/n')
      ;

抱着试试看的态度submit....

妈的! AC了。。。。

看来以后要注意了。

 

算法很简单。。

判断是否能抓到老鼠是用暴力方法判断的。

懒得写具体方法了,哈哈。

代码如下。

 

 

#include <stdio.h> #include <string.h> int main() { int c_x,c_y;//猫坐标 int m_x,m_y;//老鼠坐标 int migong[12][12]; int n,i,j,k; char c; int ctime; int c_way,m_way; while (scanf("%d",&n) != EOF) { for (k=0;k<n;k++) { memset(migong, 0, sizeof(migong)); for (i = 0;i<12;i++) { migong[11][i] = 1; migong[i][11] = 1; migong[0][i] = 1; migong[i][0] = 1; } for (i=0;i<10;i++) { for (j=0;j<10;j++) { while(scanf("%c", &c), c == '/n') ; switch (c) { case '.': migong[i+1][j+1] = 0; break; case '*': migong[i+1][j+1] = 1; break; case 'c': migong[i+1][j+1] = 0; c_x = i+1; c_y = j+1; break; case 'm': migong[i+1][j+1] = 0; m_x = i+1; m_y = j+1; break; } } } ctime = 0; m_way = 1;//1北 2东 3南 4西 c_way = 1;//1北 2东 3南 4西 while ((m_x != c_x || m_y != c_y) && ctime<=10000) { switch (c_way) { case 1: if (migong[c_x-1][c_y] == 1) c_way = 2; else c_x -= 1; break; case 2: if (migong[c_x][c_y+1] == 1) c_way = 3; else c_y += 1; break; case 3: if (migong[c_x+1][c_y] == 1) c_way = 4; else c_x += 1; break; case 4: if (migong[c_x][c_y-1] == 1) c_way = 1; else c_y -= 1; break; } switch (m_way) { case 1: if (migong[m_x-1][m_y] == 1) m_way = 2; else m_x -= 1; break; case 2: if (migong[m_x][m_y+1] == 1) m_way = 3; else m_y += 1; break; case 3: if (migong[m_x+1][m_y] == 1) m_way = 4; else m_x += 1; break; case 4: if (migong[m_x][m_y-1] == 1) m_way = 1; else m_y -= 1; break; } ctime++; } if (ctime == 10001) printf("%d/n", 0); else printf("%d/n", ctime); } } return 0; }

 

你可能感兴趣的:(c,算法)