[HDU] 4524 郑厂长系列故事——逃离迷宫
题目连接:
http://acm.hdu.edu.cn/showproblem.php?pid=4524
方法:模拟,首先根据题目描述的规则得知,要尽量消除完,必须以来就点最左边的。根据该规律进行模拟,搜先设置一个last代表上一个读入的数据,如果当前读入的数据不是第一个读入的数据那么其一定有个上一个数据last,否则其本身就last并且不再继续处理而是直接读取下一个数据。除第一个读入的数据外的其他数据A都和last比较,如果last>0且last<=该数据A,则A-=last,表示前面一个消除完了,而该位置也顺势消除了last个,同时把剩余的数量A设置为last,如果last>0且last>该数据A,就直接说明消除不完,前面一个一定还有剩余的,由于是从左往右消除的,所以前面一个的前面不再用东西了,所以前面一个一定消除不完,这里可以直接获得结果。如果last==0,则当前位置不管是不是第一个都可以当成是第一个输入数据来处理来表示从新的起点开始消除,新起点前的都顺利消除完。最后如果last不为0表示没有消除完,否则全部消除完。
代码1:
#include<iostream>
#include<queue>
#include<map>
#include<string>
#include <algorithm>
using namespace std;
int const MAX =100001;
int main()
{
int n,k,m,tc;
scanf("%d",&tc);
while(tc>0)
{
scanf("%d",&n);
int num,last;
bool get = true;
for(int i=0;i<n;i++)
{
scanf("%d",&num);
if(get)
{
if(i==0)
last = num;
else
{
if(last!=0)
{
if(last<=num)
{
num-=last;
last = num;
}
else
get=false;
}
else
last= num;
}
}
}
if(last!=0 || !get)
cout<<"I will never go out T_T"<<endl;
else
cout<<"yeah~ I escaped ^_^"<<endl;
tc--;
}
return 0;
}
代码2:
#include<iostream>
#include<queue>
#include<map>
#include<string>
#include <algorithm>
using namespace std;
int const MAX =100001;
int main()
{
int n,k,m,tc;
scanf("%d",&tc);
while(tc>0)
{
scanf("%d",&n);
int num,last;
bool get = false;
for(int i=0;i<n;i++)
{
scanf("%d",&num);
if(!get)
{
if(i==0)
last = num;
else
{
if(last>num)
{
get=true;
}
else if(last == num) //这里将前面清0的时候要注意
{
n-=(i+1);
i=-1;
last=0;
}
else
{
last = num-last;
}
}
}
}
if(last!=0 || get)
cout<<"I will never go out T_T"<<endl;
else
cout<<"yeah~ I escaped ^_^"<<endl;
tc--;
}
return 0;
}
感想:对于那种在输入的过程中就可以得知最后结果的题目要注意,及时输入接受完成前得知了最后结果,也要把输入全部接受完。否则wa
浙公网安备 33010602011771号