| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
送上代码和注释#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
const int MAXN_X=73; //13+(6-1)*12
const int MAXN=1005;
struct node
{
char name[20]; //名字
int x,y; //坐标
int Move; //判断坦克是否移动
int facing; //方向
int exist; //生存
};
node tank[MAXN+5]; //坦克
node shoot[MAXN+5]; //子弹
int tankmap[MAXN_X+5][MAXN_X+5]; //坦克分布图
int shootmap[MAXN_X+5][MAXN_X+5]; //子弹分布图
struct node1
{
int time; //时间
int Move; //判断坦克是否移动
char name[20]; //名字
char content[20]; //内容
int angle; //角度
}cmd[MAXN+5];
int main()
{
int n,m; //n为坦克数量,m为命令数
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n==0&&m==0)break;
memset(shootmap,-1,sizeof(shootmap));
memset(tankmap,-1,sizeof(tankmap));
for(int i=0;i<n;i++)
{
scanf("%s%d%d%d",tank[i].name,&tank[i].x,&tank[i].y,&tank[i].facing);
tank[i].x=tank[i].x*6/10; //离散化数组,坦克的速度为10像素
tank[i].y=tank[i].y*6/10;
tankmap[tank[i].x][tank[i].y]=i; //加入坦克分布数组里面,标记该位置有坦克i
tank[i].exist=1; //i坦克存活
tank[i].Move=0; //都初始为0移动
}
for(int i=0;i<m;i++)
{
scanf("%d%s%s",&cmd[i].time,cmd[i].name,cmd[i].content);
if(strcmp(cmd[i].content,"TURN")==0)
{
scanf("%d",&cmd[i].angle); //移动的角度
}
cmd[i].time*=6; //同样离散化扩大6倍
}
int totalshoot=0; //总共子弹数
int t=0; //第t时间
int cmd_count=0; //读了第几条命令
while(1)
{
for(int i=0;i<MAXN_X;i++) //每次都更新子弹的数组为-1,然后在进行更新
{
for(int j=0;j<MAXN_X;j++)
{
shootmap[i][j]=-1;
}
}
for(int i=0;i<totalshoot;i++)
{
if(shoot[i].exist==1) //更新子弹分布图
{
shootmap[shoot[i].x][shoot[i].y]=1;
}
}
for(int i=0;i<MAXN_X;i++)
{
for(int j=0;j<MAXN_X;j++)
{
if(tankmap[i][j]!=-1&&shootmap[i][j]!=-1)
{
for(int k=0;k<totalshoot;k++)
{
if(shoot[k].exist==1&&shoot[k].x==i&&shoot[k].y==j) //子弹存在,并且等于坦克的坐标就删除该子弹,同时结束循环后坦克也要死
{
shoot[k].exist=0;
}
}
tank[tankmap[i][j]].exist=0; //该坦克被打死
tankmap[i][j]=shootmap[i][j]=-1; //更新地图
}
}
}
while(cmd_count<m&&cmd[cmd_count].time==t) //在t时间的时候,并且执行命令
{
for(int j=0;j<n;j++)
{
if(tank[j].exist==1&&strcmp(tank[j].name,cmd[cmd_count].name)==0) //i坦克存在,并且名字和命令中的名字相同
{
if(strcmp(cmd[cmd_count].content,"MOVE")==0) //坦克移动
{
tank[j].Move=1; //移动为1
}
else if(strcmp(cmd[cmd_count].content,"STOP")==0) //坦克停止
{
tank[j].Move=0; //停止为0
}
else if(strcmp(cmd[cmd_count].content,"TURN")==0) //调整角度
{
tank[j].facing=(tank[j].facing+cmd[cmd_count].angle+360)%360; //根据题目所给出的来计算角度
}
else if(strcmp(cmd[cmd_count].content,"SHOOT")==0) //射出子弹
{
shoot[totalshoot].x=tank[j].x; //子弹初始位置为坦克位置
shoot[totalshoot].y=tank[j].y;
shoot[totalshoot].facing=tank[j].facing; //子弹射出的角度为坦克对准的角度
shoot[totalshoot].exist=1; //子弹存在
totalshoot++; //累加----计算有多少子弹
}
break;
}
}
cmd_count++; //读下一条命令
}
for(int i=0;i<totalshoot;i++) //更新子弹的状态
{
if(shoot[i].exist==1) //更新子弹的前提就是子弹存在
{
if(shoot[i].facing==0) //子弹的角度为0,往右
{
if(shoot[i].x+2<MAXN_X)
{
shoot[i].x+=2; //加二是因为,子弹的速度为20像素,也就是2个格子
}
else //不满足就是超出范围了,子弹就要消失
{
shoot[i].exist=0;
}
}
else if(shoot[i].facing==90)//往上
{
if(shoot[i].y+2<MAXN_X)
{
shoot[i].y+=2;
}
else
{
shoot[i].exist=0;
}
}
else if(shoot[i].facing==180) //往左
{
if(shoot[i].x-2<MAXN_X&&shoot[i].x-2>=0)
{
shoot[i].x-=2;
}
else
{
shoot[i].exist=0;
}
}
else if(shoot[i].facing==270) //往下
{
if(shoot[i].y-2<MAXN_X&&shoot[i].y-2>=0)
{
shoot[i].y-=2;
}
else
{
shoot[i].exist=0;
}
}
}
}
for(int i=0;i<n;i++) //更新坦克移动的状态
{
if(tank[i].exist==1&&tank[i].Move==1) //更新坦克的时候,前提就是坦克要存在,并且该坦克标记了要移动
{
tankmap[tank[i].x][tank[i].y]=-1; //既然移动就要把当前位置给删除
if(tank[i].facing==0&&tank[i].x+1<MAXN_X) //看角度为多少,为0就是往右移动
{
tank[i].x++; //移动加1
}
else if(tank[i].facing==90&&tank[i].y+1<MAXN_X) //往上
{
tank[i].y++;
}
else if(tank[i].facing==180&&tank[i].x-1>=0) //往左
{
tank[i].x--; //减一
}
else if(tank[i].facing==270&&tank[i].y-1>=0) //往下
{
tank[i].y--; //减一
}
tankmap[tank[i].x][tank[i].y]=i; //然后在更新i坦克的新位置
}
}
if(cmd_count==m) //如果命令执行完了,就看子弹都飞完了吗,如果子弹没有飞完就继续执行,否则结束循环
{
int e=1;
for(int i=0;i<totalshoot;i++)
{
if(shoot[i].exist==1)
{
e=0;
break;
}
}
if(e)
{
break;
}
}
t++; //下一时刻的命令
}
char Win_Tank[20];
int jude=0;
for(int i=0;i<n;i++)
{
if(tank[i].exist==1) //找存在的坦克,如果有两个以上的坦克就没有胜利者
{
jude++;
strcpy(Win_Tank,tank[i].name);
}
}
if(jude==1)
{
printf("%s\n",Win_Tank); //否则输出胜利者
}
else
{
printf("NO WINNER!\n");
}
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator