來自hoj 1009 fat cat。
原題Fat cat's duty is to catch mice, and it controls several rooms arranged in a matrix in which mice often come. Now a mouse comes into this room matrix and digs many many holes, and the mouse is cunning and it knows that fat cat will sleep in the daytime, so it goes out for food in that time instead of at night. Fat cat has to put some cheese in each room to make it out.
The mouse always appears first at room(0,0), and it will turn to another room near the current room which has the most number of cheese after it eats up the cheese in the room. If there is no adjacent room or the number of cheese is 0 in every adjacent room, the mouse quickly run in a hole after it eats the cheese and fat cat can't catch it.
When the mouse and the cat arrive at the same room or the mouse turns to a room where the cat is at the same time, the cat can catch the mouse!
When the mouse first appears, fat cat is in room(i,j)(not in room(0,0)),and it wants to catch the mouse as quickly as it can before the mouse runs away. We suppose that both the cat and the mouse move only vertically or horizontally 1 step in each time period.
Now it's your turn to write a program to calculate how many steps at least fat cat should move to catch the mouse. For example: the mouse starts at room(0,0), and fat cat is in room(2,0) at the same time. And the mouse moves to room(0,1),while fat cat can go to either room(2,1) or room(1,0), or just stay there.
Input
the input contains mutiple cases. The first line of each case consists of two positive integer: n and m (1<n<=100,1<m<=100),showing that the room matrix has n rows and m columns rooms. Then will be n rows non_negative integers presenting the number of cheese in each room(there are no two rooms have the same positive number of cheese). Each row has m integers. The following row shows fat cat's initial position: row i and column j.
Output
for each case,print the minimum number of time period fat cat uses to catch the mouse in a single line. If the mouse run away before being caught, print "impossible" instead.
Sample Input
2 2
15 3
2 4
0 1
3 3
1 5 9
3 7 0
2 4 6
2 0
Sample Output
1
impossible
我的程序如下。然而OJ無限WA。反正給出的用例沒問題來著。菜雞真的求助。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int MAX(int a,int b,int c,int d);
int main()
{
int matrix[100][100],flag;
memset(matrix,0,sizeof(matrix));
int c,r;
int i,j;
int a,b;
int m,n;
int step=0;
while(scanf("%d %d",&r,&c)==2&&r>0&&c>0)
{
step=0;
for(i=0; i<r; i++)
{
for(j=0; j<c; j++)
{
scanf("%d",&matrix[i][j]);
}
}
a=0,b=0;
scanf("%d %d",&m,&n);
if(m==0&&n==0)
{
printf("impossible\n");
continue;
}
else if(m>=r||n>=c)
{
printf("impossible\n");
continue;
}
while(matrix[a][b]!=0)
{
matrix[a][b]=0;
if(a-1>=0)
{
if(b-1>=0)
{
flag=MAX(matrix[a-1][b],matrix[a][b-1],matrix[a][b+1],matrix[a+1][b]);
switch (flag)
{
case 1:
a=a-1;
b=b;
break;
case 2:
a=a;
b=b-1;
break;
case 3:
a=a;
b=b+1;
break;
case 4:
a=a+1;
b=b;
break;
}
}
else
{
flag=MAX(matrix[a-1][b],0,matrix[a][b+1],matrix[a+1][b]);
switch (flag)
{
case 1:
a=a-1;
b=b;
break;
case 2:
a=a;
b=b;
break;
case 3:
a=a;
b=b+1;
break;
case 4:
a=a+1;
b=b;
break;
}
}
}
else
{
if(b-1>=0)
{
flag=MAX(0,matrix[a][b-1],matrix[a][b+1],matrix[a+1][b]);
switch (flag)
{
case 1:
a=a;
b=b;
break;
case 2:
a=a;
b=b-1;
break;
case 3:
a=a;
b=b+1;
break;
case 4:
a=a+1;
b=b;
break;
}
}
else
{
flag=MAX(0,0,matrix[a][b+1],matrix[a+1][b]);
switch (flag)
{
case 1:
a=a;
b=b;
break;
case 2:
a=a;
b=b;
break;
case 3:
a=a;
b=b+1;
break;
case 4:
a=a+1;
b=b;
break;
}
}
}
step++;
if(step>=(fabs(m-a)+fabs(n-b)))
{
printf("%d\n",step);
break;
}
else if(matrix[a][b]==0)
{
printf("impossible\n");
}
}
}
return 0;
}
int MAX(int a,int b,int c,int d)
{
int max;
max=a;
if(max<b)
{
max=b;
}
if(max<c)
{
max=c;
}
if(max<d)
{
max=d;
}
if(max==a)
{
return 1;
}
else if(max==b)
{
return 2;
}
else if(max==c)
{
return 3;
}
else if(max==d)
{
return 4;
}
}#include <stdlib.h>
#include <iostream>
using namespace std;
const int maxn = 102;
int a[maxn][maxn];
int si,sj;
int n,m;
int dx[]= {-1,0,1,0};
int dy[]={0,-1,0,1};
int f( int a,int b) {return a>b? (a-b):(b-a);}
void bfs(int x,int y,int time,int &ok)
{
a[x][y]=0; int r = 0,c=0,max =0;
for ( int i =0;i<4;++i )
if (x+dx[i]>=0&&x+dx[i]<n&&y+dy[i]>=0&&y+dy[i]<m && a[x+dx[i]][y+dy[i]]>max )
{
r= x+dx[i];c=y+dy[i]; max=a[r][c];
}
if (!r&&!c) return;
if (f(si,r)+f(sj,c)<=time ) {ok =time;return;}
bfs(r,c,time+1,ok);
}
int main(int argc, char** argv) {
while ( cin>>n>>m&&n>0&&m>0)
{
for ( int i =0;i<n;++i )
for ( int j =0;j<m;++j) cin>>a[i][j];
cin>>si>>sj;
int ok =0;
bfs(0,0,1,ok);
if ( ok) cout<<ok<<endl;
else cout<<"impossible\n";
}
return (EXIT_SUCCESS);
}北大青鳥APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國家
達內(nèi)教育集團成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機構(gòu),是中國一站式人才培養(yǎng)平臺、一站式人才輸送平臺。2014年4月3日在美國成功上市,融資1
北大課工場是北京大學(xué)校辦產(chǎn)業(yè)為響應(yīng)國家深化產(chǎn)教融合/校企合作的政策,積極推進“中國制造2025”,實現(xiàn)中華民族偉大復(fù)興的升級產(chǎn)業(yè)鏈。利用北京大學(xué)優(yōu)質(zhì)教育資源及背
博為峰,中國職業(yè)人才培訓(xùn)領(lǐng)域的先行者
曾工作于聯(lián)想擔(dān)任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項目經(jīng)理從事移動互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負責(zé)iOS教學(xué)及管理工作。
浪潮集團項目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺面向?qū)ο箝_發(fā)經(jīng)驗,技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點難點突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫,具有快速界面開發(fā)的能力,對瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁制作和網(wǎng)頁游戲開發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開發(fā)經(jīng)驗。曾經(jīng)歷任德國Software AG 技術(shù)顧問,美國Dachieve 系統(tǒng)架構(gòu)師,美國AngelEngineers Inc. 系統(tǒng)架構(gòu)師。