UVA 1622 Robot
生活随笔
收集整理的這篇文章主要介紹了
UVA 1622 Robot
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題意:
給出n*m個格子,每個格子里有一個機器人,可以執行東南西北四種指令,但是移動出格就會爆炸。給出四種指令的個數,求最多完成多少次指令。?
分析:
首先對輸入數據進行處理,使得cw≥ce、cn≥cs且先執行東西方向的來回移動比先執行南北方向來回移動更佳。然后執行東西移動,然后排序,對比每次向西移動還是開始南北移動更好。當僅剩西和北兩個方向后,模擬至結束。
代碼:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long LL;
int main()
{
LL n,m,t=0;
while(cin>>n>>m,m||n)
{
t++;
LL cn,cs,cw,ce,ans=0;
scanf("%lld%lld%lld%lld",&cn,&cs,&cw,&ce);
if(cn<cs)
swap(cs,cn);
if(cw<ce)
swap(ce,cw);
LL t1=n+(m-1)*n*ce*2+(m-1)+(m-1)*(n-1)*cs*2;
LL t2=m+m*(n-1)*cs*2+(n-1)+(m-1)*(n-1)*ce*2;
if(cw-ce)
{
t1+=(m-1)*n;
t2+=(m-1)*(n-1);
}
if(cn-cs)
{
t1+=(m-1)*(n-1);
t2+=m*(n-1);
}
if(t1<t2)
{
swap(m,n);
swap(cn,cw);
swap(cs,ce);
}
int flag=1;
if(ce)
{
ans+=n+(m-1)*n*ce*2;
cw-=ce;
ce=0;
m--;
flag=0;
}
if(cw)
{
ans+=m*n;
--cw;
if(flag)
--m;
}
cw=min(m,cw);
while(cw||cn)
{
if(cs)
{
LL t1=m*n+(n-1)*m*2*cs;
LL t2=m*n+(m-1)*n+(m-1)*(n-1)*(2*cs-1);
if(cn-cs)
{
t1=m*n+(n-1)*m*(2*cs+1);
t2=m*n+(m-1)*n+(m-1)*(n-1)*2*cs;
}
if(t1>t2||!cw)
{
ans+=m+m*(n-1)*cs*2;
cn-=cs;
cs=0;
--n;
if(cn)
ans+=m*n,--cn;
cn=min(n,cn);
}
else
{
ans+=m*n;
--m;
--cw;
}
}
else if(!cw)
ans+=m*cn*(2*n-cn+1)/2,cn=0;
else if(!cn)
ans+=n*cw*(2*m-cw+1)/2,cw=0;
else
{
ans+=m*n;
if(m>n) --m,--cw;
else --n,--cn;
}
}
printf("Case %d: ",t);
printf("%lld\n",ans);
}
return 0;
}
轉載于:https://www.cnblogs.com/137033036-wjl/p/4945009.html
總結
以上是生活随笔為你收集整理的UVA 1622 Robot的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++ string字符串比较问题
- 下一篇: [LeetCode] Binary Tr