Will it be an endless loop?
Because I get TLE,and i really don't know why
.I gave it 10000 life and it finished immediately.Maybe there is something wrong with my code:
Code: Select all
#include <stdio.h>
class bumper
{
public:
int value;
int cost;
};
void main()
{
int i=0,j=0;
bumper allbumper[51][51];
bool hb[51][51];
int top=0,right=0;
int hwcost=0;
int bumpers=0;
int exit=0;
int x=0,y=0,life=0,way=0;
int value=0;
int total=0;
int flag=0;
int tx,ty;
scanf("%d %d",&right,&top);
right--;
top--;
scanf("%d",&hwcost);
scanf("%d",&bumpers);
for(i=0;i<51;i++)
for(j=0;j<51;j++)
hb[i][j]=0;
for(i=0;i<bumpers;i++)
{
scanf("%d %d",&tx,&ty);
scanf("%d %d",&allbumper[tx][ty].value,&allbumper[tx][ty].cost);
hb[tx][ty]=1;
}
while(scanf("%d %d %d %d",&x,&y,&way,&life)!=EOF)
{
value=0;
do
{
exit=0;
flag=0;
switch(way)
{
case 0:
life--;
if(life==0)
{
exit=1;
printf("%dn",value);
break;
}
if(x+1>right)
{
way=(way+3)%4;
if(life>hwcost)
{
life=life-hwcost;
break;
}
else
{
exit=1;
printf("%dn",value);
break;
}
}
else
{
for(i=0;i<bumpers;i++)
{
if(hb[x+1][y]==1)
{
flag=1;
way=(way+3)%4;
if(life<allbumper[x+1][y].cost)
{
exit=1;
printf("%dn",value);
break;
}
else if(life==allbumper[x+1][y].cost)
{
value=value+allbumper[x+1][y].value;
printf("%dn",value);
exit=1;
break;
}
else if(life>allbumper[x+1][y].cost)
{
life=life-allbumper[x+1][y].cost;
value=allbumper[x+1][y].value+value;
break;
}
}
}
if(flag==0)
x++;
break;
}
case 1:
life--;
if(life==0)
{
exit=1;
printf("%dn",value);
break;
}
if(y+1>top)
{
way=(way+3)%4;
if(life>hwcost)
{
life=life-hwcost;
break;
}
else
{
exit=1;
printf("%dn",value);
break;
}
}
else
{
for(i=0;i<bumpers;i++)
{
if(hb[x][y+1]==1)
{
flag=1;
way=(way+3)%4;
if(life<allbumper[x][y+1].cost)
{
exit=1;
printf("%dn",value);
break;
}
else if(life==allbumper[x][y+1].cost)
{
value=value+allbumper[x][y+1].value;
printf("%dn",value);
exit=1;
break;
}
else if(life>allbumper[x][y+1].cost)
{
life=life-allbumper[x][y+1].cost;
value=allbumper[x][y+1].value+value;
break;
}
}
}
if(flag==0)
y++;
break;
}
case 2:
life--;
if(life==0)
{
exit=1;
printf("%dn",value);
break;
}
if(x-1<0)
{
way=(way+3)%4;
if(life>hwcost)
{
life=life-hwcost;
break;
}
else
{
exit=1;
printf("%dn",value);
break;
}
}
else
{
for(i=0;i<bumpers;i++)
{
if(hb[x-1][y]==1)
{
flag=1;
way=(way+3)%4;
if(life<allbumper[x-1][y].cost)
{
exit=1;
printf("%dn",value);
break;
}
else if(life==allbumper[x-1][y].cost)
{
value=value+allbumper[x-1][y].value;
printf("%dn",value);
exit=1;
break;
}
else if(life>allbumper[x-1][y].cost)
{
life=life-allbumper[x-1][y].cost;
value=allbumper[x-1][y].value+value;
break;
}
}
}
if(flag==0)
x--;
break;
}
case 3:
life--;
if(life==0)
{
exit=1;
printf("%dn",value);
break;
}
if(y-1<0)
{
way=(way+3)%4;
if(life>hwcost)
{
life=life-hwcost;
break;
}
else
{
exit=1;
printf("%dn",value);
break;
}
}
else
{
for(i=0;i<bumpers;i++)
{
if(hb[x][y-1]==1)
{
flag=1;
way=(way+3)%4;
if(life<allbumper[x][y-1].cost)
{
exit=1;
printf("%dn",value);
break;
}
else if(life==allbumper[x][y-1].cost)
{
value=value+allbumper[x][y-1].value;
printf("%dn",value);
exit=1;
break;
}
else if(life>allbumper[x][y-1].cost)
{
life=life-allbumper[x][y-1].cost;
value=allbumper[x][y-1].value+value;
break;
}
}
}
if(flag==0)
y--;
break;
}
}
}while(exit==0);
total=total+value;
}
printf("%dn",total);
}