hihocoder1426||UVALive - 7672 What a Ridiculous Election
2017-11-11 22:24
344 查看
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=5694
16北京现场赛题目
题意:
每次有三种可行操作
1:交换相邻两个数位
2:把某一个数位加1,取模10
3:把某一个数位乘2,取模10
2操作最多能用3次,3操作最多能用2次
给一个五个字符的字符串,问12345最少要几次操作后得到给的字符串
搜索预处理一下就可以了,起初想法就是,因为交换数位并不影响2,3操作,所以先BFS预处理12345只进行1操作后到达每个状态的最少操作次数
然后在从这每个状态向下暴力2,3操作就可以了
自己埋了个坑。。。。。。
一直用一个dp数组记录的,因此BFS预处理的那些状态的值可能会因为第二步暴力时改变,这样显然不可以,因为没有记录暴力改变已经用掉的2,3操作的次数
开个vector暂存一下BFS处理出来的即可
#include<bits/stdc++.h>
using namespace std;
const int inf=0x3f3f3f3f;
int dp[200000];
int a[6],b[6];
struct node
{
int state,cnt;
};
void BFS()
{
node now;
now.state=12345,now.cnt=dp[12345]=0;
queue<node>Q;
Q.push(now);
while(!Q.empty())
{
node pe=Q.front();
Q.pop();
int i,k,temp=pe.state;
for(i=0;i<5;i++)
{
a[i]=temp%10;
temp/=10;
}
for(i=0;i<5;i++)
b[i]=a[i];
for(i=0;i<4;i++)
{
swap(b[i],b[i+1]);
int now=0,t=1;
for(k=0;k<5;k++)
now+=t*b[k],t*=10;
if(dp[now]==inf)
{
node ne;
ne.state=now;
dp[now]=pe.cnt+1;
ne.cnt=pe.cnt+1;
Q.push(ne);
}
swap(b[i],b[i+1]);
}
}
}
char ch[6];
int need;
void dfs(int state,int l1,int l2,int cost)
{
dp[state]=min(dp[state],need+cost);
if(l1==0&&l2==0)
return ;
int st=state,t=1;
for(int i=4;i>=0;i--)
{
int temp=st%10;
if(l1)
dfs(state-temp*t+(temp+1)%10*t,l1-1,l2,cost+1);
if(l2)
dfs(state-temp*t+(temp*2)%10*t,l1,l2-1,cost+1);
st/=10;
t*=10;
}
return ;
}
vector<node>w;
int main()
{
memset(dp,inf,sizeof(dp));
BFS();
for(int i=1;i<100000;i++)
if(dp[i]!=inf)
{
node temp;
temp.state=i,temp.cnt=dp[i];
w.push_back(temp);
}
for(int i=0;i<w.size();i++)
need=w[i].cnt,dfs(w[i].state,3,2,0);
while(~scanf("%s",ch))
{
int state=0,t=1;
for(int i=4;i>=0;i--)
{
state+=(ch[i]-'0')*t,t*=10;
}
if(dp[state]==inf)
printf("-1\n");
else
printf("%d\n",dp[state]);
}
return 0;
}
16北京现场赛题目
题意:
每次有三种可行操作
1:交换相邻两个数位
2:把某一个数位加1,取模10
3:把某一个数位乘2,取模10
2操作最多能用3次,3操作最多能用2次
给一个五个字符的字符串,问12345最少要几次操作后得到给的字符串
搜索预处理一下就可以了,起初想法就是,因为交换数位并不影响2,3操作,所以先BFS预处理12345只进行1操作后到达每个状态的最少操作次数
然后在从这每个状态向下暴力2,3操作就可以了
自己埋了个坑。。。。。。
一直用一个dp数组记录的,因此BFS预处理的那些状态的值可能会因为第二步暴力时改变,这样显然不可以,因为没有记录暴力改变已经用掉的2,3操作的次数
开个vector暂存一下BFS处理出来的即可
#include<bits/stdc++.h>
using namespace std;
const int inf=0x3f3f3f3f;
int dp[200000];
int a[6],b[6];
struct node
{
int state,cnt;
};
void BFS()
{
node now;
now.state=12345,now.cnt=dp[12345]=0;
queue<node>Q;
Q.push(now);
while(!Q.empty())
{
node pe=Q.front();
Q.pop();
int i,k,temp=pe.state;
for(i=0;i<5;i++)
{
a[i]=temp%10;
temp/=10;
}
for(i=0;i<5;i++)
b[i]=a[i];
for(i=0;i<4;i++)
{
swap(b[i],b[i+1]);
int now=0,t=1;
for(k=0;k<5;k++)
now+=t*b[k],t*=10;
if(dp[now]==inf)
{
node ne;
ne.state=now;
dp[now]=pe.cnt+1;
ne.cnt=pe.cnt+1;
Q.push(ne);
}
swap(b[i],b[i+1]);
}
}
}
char ch[6];
int need;
void dfs(int state,int l1,int l2,int cost)
{
dp[state]=min(dp[state],need+cost);
if(l1==0&&l2==0)
return ;
int st=state,t=1;
for(int i=4;i>=0;i--)
{
int temp=st%10;
if(l1)
dfs(state-temp*t+(temp+1)%10*t,l1-1,l2,cost+1);
if(l2)
dfs(state-temp*t+(temp*2)%10*t,l1,l2-1,cost+1);
st/=10;
t*=10;
}
return ;
}
vector<node>w;
int main()
{
memset(dp,inf,sizeof(dp));
BFS();
for(int i=1;i<100000;i++)
if(dp[i]!=inf)
{
node temp;
temp.state=i,temp.cnt=dp[i];
w.push_back(temp);
}
for(int i=0;i<w.size();i++)
need=w[i].cnt,dfs(w[i].state,3,2,0);
while(~scanf("%s",ch))
{
int state=0,t=1;
for(int i=4;i>=0;i--)
{
state+=(ch[i]-'0')*t,t*=10;
}
if(dp[state]==inf)
printf("-1\n");
else
printf("%d\n",dp[state]);
}
return 0;
}
相关文章推荐
- UVALive - 7271 A Math Problem (hihocoder 1259)
- UVALive - 3708 Graveyard
- UVaLive/LA 6801 Sequence(DP)
- Islands in the Data Stream UVALive - 7092
- Confusing Date Format UVALive 7711 给定mm-mm-mm格式的时间。年份(1900-1999)只给了后两位数,问有多少种合法的排列使时间正确。
- uva live 6183 Stylish
- UVALive 6190
- 【UVALive】4287 Proving Equivalences 强连通分量
- UVALive 4235 Recursively Palindromic Partitions
- UVALive 6450 Social Advertising DFS解法
- UVALive 7501 Business Cycle(二分,xjb乱判)
- 【排列组合】UVALive - 6926 Maximum Score
- UVALive 6133_Cellphone Typing题解
- UVALIVE 2322 Wooden Sticks
- UVALive - 7511 Multiplication Table (模拟)
- UVALive 5103 / HDU 3695 Computer Virus on Planet Pandora(AC自动机裸)
- UVALive-6665-Dragons Cruller(Dij+Hash)
- UVaLive 4254 Processor (二分+优先队列)
- UVa 1513 / UVALive 5902 Movie collection (树状数组)
- UVAlive-6577 Binary Tree(递推+找规律)