UVALive - 7672 - What a Ridiculous Election(搜索预处理)
2017-11-15 16:53
411 查看
In country Light Tower, a presidential election is going on. There are two candidates, Mr. X1
andMr. X2, and both of them
are not like good persons. One is called a liar and the other is called amaniac. They tear(Chinese English word, means defame) each other on TV face to face, on newspaper,on internet
. . . on all kinds of media. The country is tore into two parts because the people who supportX1
are almost as many as the people who support X2.
After the election day, X1
and X2
get almost the same number of votes. No one gets enough votes towin. According to the law of the country, the Great Judge must decide who will be the president. Butthe judge
doesn’t want to o end half population of the country, so he randomly chooses a 6 years oldkid Tom and authorize him to pick the president. Sounds weird? But the democracy in Light Tower isjust like that.
The poor or lucky little kid Tom doesn’t understand what is happening to his country. But he hashis way to do his job. Tom’s ao shu(Chinese English word, means some kind of weird
math for kids)teacher just left him a puzzle a few days ago, Tom decide that he who solve that puzzle in a better waywill be president. The ao shu teacher’s puzzle is like this:
Given a string which consists of ve digits(‘0’..‘9’),
like “02943”, you should change “12345”
into itby as few as possible operations. There are 3 kinds of operations:
1. Swap two adjacent digits.
2. Increase a digit by one. If the result exceed 9, change it to it modulo 10.3. Double a digit. If the result exceed 9, change it to it modulo 10.
You can use operation 2 at most three times, and use operation 3 at most twice.
As a melon eater (Chinese English again, means bystander), which candidate do you support?Please help him solve the puzzle.
Input
There are no more than 100,000 test cases.
Each test case is a string which consists of 5 digits.
Output
For each case, print the minimum number of operations must be used to change “12345”
into the givenstring. If there is no solution, print ‘-1’.
Sample Input
Sample Output
1
-1
3
题意:将“12345”变为所给串所需要的最少操作次数,若无法变成目标数则输出"-1"
操作有三种:1、交换相邻的两个数字
2、某一位数字加一(模10) 3、某一位数字乘二(模10)
其中操作2最多使用三次,操作3最多使用两次
若所有状态都可以达到最多有1e5*3*4种情况,不是很多,所以可以先将所有的情况求出,利用DFS或BFS
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
const int N = 99999 + 10;
const int INF = 0x3f3f3f3f;
int Time
[5][5];
void dfs(char s[],int dou,int add,int cnt){
int sum = 0;
for(int i=0;i<5;i++) sum = sum * 10 + s[i] - '0';
if(Time[sum][add][dou]>cnt){
Time[sum][add][dou] = cnt;
}else return;
char str[10];
for(int i=0;i<5;i++){
if(dou<2){
for(int j=0;j<5;j++) str[j] = s[j];
str[i] = (str[i]-'0') * 2 % 10 + '0';
dfs(str,dou+1,add,cnt+1);
}
if(add<3){
for(int j=0;j<5;j++) str[j] = s[j];
str[i] = (str[i] - '0' + 1) % 10 + '0';
dfs(str,dou,add+1,cnt+1);
}
}
for(int i=0;i<4;i++){
swap(s[i],s[i+1]);
for(int j=0;j<5;j++) str[j] = s[j];
dfs(str,dou,add,cnt+1);
swap(s[i],s[i+1]);
}
}
struct node{
int num,add,dou,cnt;
node(int a,int b,int c,int d){
num = a;add = b;dou = c;cnt = d;
}
};
void bfs(){
node st(12345,0,0,0);
queue<node>q;
q.push(st);
Time[12345][0][0] = 0;
while(!q.empty()){
st = q.front();q.pop();
int num[10],n = st.num;
for(int i=4;i>=0;i--) {num[i] = n%10;n/=10;}
for(int i=0;i<5;i++){
int temp = num[i];
if(st.add<3){
n = 0;
num[i]++; num[i]%=10;
for(int j=0;j<5;j++) n = n*10 + num[j];
if(Time
[st.add+1][st.dou]==INF){
Time
[st.add+1][st.dou] = st.cnt + 1;
q.push(node(n,st.add+1,st.dou,st.cnt+1));
}
4000
num[i] = temp;
}
if(st.dou<2){
n = 0;
num[i] *= 2; num[i]%=10;
for(int j=0;j<5;j++) n = n*10 + num[j];
if(Time
[st.add][st.dou+1]==INF){
Time
[st.add][st.dou+1] = st.cnt + 1;
q.push(node(n,st.add,st.dou+1,st.cnt+1));
}
num[i] = temp;
}
}
for(int i=0;i<4;i++){
n = 0;
swap(num[i],num[i+1]);
for(int j=0;j<5;j++) n = n*10 + num[j];
if(Time
[st.add][st.dou]==INF){
Time
[st.add][st.dou] = st.cnt + 1;
q.push(node(n,st.add,st.dou,st.cnt+1));
}
swap(num[i],num[i+1]);
}
}
}
int main()
{
memset(Time,INF,sizeof Time);
char s[10]="12345";
// dfs(s,0,0,0);
bfs();
while(scanf("%s",s)!=EOF){
int num = 0;
for(int i=0;i<5;i++) num = num * 10 + s[i] - '0';
int ans = INF;
for(int i=0;i<=3;i++){
for(int j=0;j<=2;j++){
ans = min(ans,Time[num][i][j]);
}
}
if(ans == INF) printf("-1\n");
else printf("%d\n",ans);
}
return 0;
}
andMr. X2, and both of them
are not like good persons. One is called a liar and the other is called amaniac. They tear(Chinese English word, means defame) each other on TV face to face, on newspaper,on internet
. . . on all kinds of media. The country is tore into two parts because the people who supportX1
are almost as many as the people who support X2.
After the election day, X1
and X2
get almost the same number of votes. No one gets enough votes towin. According to the law of the country, the Great Judge must decide who will be the president. Butthe judge
doesn’t want to o end half population of the country, so he randomly chooses a 6 years oldkid Tom and authorize him to pick the president. Sounds weird? But the democracy in Light Tower isjust like that.
The poor or lucky little kid Tom doesn’t understand what is happening to his country. But he hashis way to do his job. Tom’s ao shu(Chinese English word, means some kind of weird
math for kids)teacher just left him a puzzle a few days ago, Tom decide that he who solve that puzzle in a better waywill be president. The ao shu teacher’s puzzle is like this:
Given a string which consists of ve digits(‘0’..‘9’),
like “02943”, you should change “12345”
into itby as few as possible operations. There are 3 kinds of operations:
1. Swap two adjacent digits.
2. Increase a digit by one. If the result exceed 9, change it to it modulo 10.3. Double a digit. If the result exceed 9, change it to it modulo 10.
You can use operation 2 at most three times, and use operation 3 at most twice.
As a melon eater (Chinese English again, means bystander), which candidate do you support?Please help him solve the puzzle.
Input
There are no more than 100,000 test cases.
Each test case is a string which consists of 5 digits.
Output
For each case, print the minimum number of operations must be used to change “12345”
into the givenstring. If there is no solution, print ‘-1’.
Sample Input
12435 99999 12374
Sample Output
1
-1
3
题意:将“12345”变为所给串所需要的最少操作次数,若无法变成目标数则输出"-1"
操作有三种:1、交换相邻的两个数字
2、某一位数字加一(模10) 3、某一位数字乘二(模10)
其中操作2最多使用三次,操作3最多使用两次
若所有状态都可以达到最多有1e5*3*4种情况,不是很多,所以可以先将所有的情况求出,利用DFS或BFS
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
const int N = 99999 + 10;
const int INF = 0x3f3f3f3f;
int Time
[5][5];
void dfs(char s[],int dou,int add,int cnt){
int sum = 0;
for(int i=0;i<5;i++) sum = sum * 10 + s[i] - '0';
if(Time[sum][add][dou]>cnt){
Time[sum][add][dou] = cnt;
}else return;
char str[10];
for(int i=0;i<5;i++){
if(dou<2){
for(int j=0;j<5;j++) str[j] = s[j];
str[i] = (str[i]-'0') * 2 % 10 + '0';
dfs(str,dou+1,add,cnt+1);
}
if(add<3){
for(int j=0;j<5;j++) str[j] = s[j];
str[i] = (str[i] - '0' + 1) % 10 + '0';
dfs(str,dou,add+1,cnt+1);
}
}
for(int i=0;i<4;i++){
swap(s[i],s[i+1]);
for(int j=0;j<5;j++) str[j] = s[j];
dfs(str,dou,add,cnt+1);
swap(s[i],s[i+1]);
}
}
struct node{
int num,add,dou,cnt;
node(int a,int b,int c,int d){
num = a;add = b;dou = c;cnt = d;
}
};
void bfs(){
node st(12345,0,0,0);
queue<node>q;
q.push(st);
Time[12345][0][0] = 0;
while(!q.empty()){
st = q.front();q.pop();
int num[10],n = st.num;
for(int i=4;i>=0;i--) {num[i] = n%10;n/=10;}
for(int i=0;i<5;i++){
int temp = num[i];
if(st.add<3){
n = 0;
num[i]++; num[i]%=10;
for(int j=0;j<5;j++) n = n*10 + num[j];
if(Time
[st.add+1][st.dou]==INF){
Time
[st.add+1][st.dou] = st.cnt + 1;
q.push(node(n,st.add+1,st.dou,st.cnt+1));
}
4000
num[i] = temp;
}
if(st.dou<2){
n = 0;
num[i] *= 2; num[i]%=10;
for(int j=0;j<5;j++) n = n*10 + num[j];
if(Time
[st.add][st.dou+1]==INF){
Time
[st.add][st.dou+1] = st.cnt + 1;
q.push(node(n,st.add,st.dou+1,st.cnt+1));
}
num[i] = temp;
}
}
for(int i=0;i<4;i++){
n = 0;
swap(num[i],num[i+1]);
for(int j=0;j<5;j++) n = n*10 + num[j];
if(Time
[st.add][st.dou]==INF){
Time
[st.add][st.dou] = st.cnt + 1;
q.push(node(n,st.add,st.dou,st.cnt+1));
}
swap(num[i],num[i+1]);
}
}
}
int main()
{
memset(Time,INF,sizeof Time);
char s[10]="12345";
// dfs(s,0,0,0);
bfs();
while(scanf("%s",s)!=EOF){
int num = 0;
for(int i=0;i<5;i++) num = num * 10 + s[i] - '0';
int ans = INF;
for(int i=0;i<=3;i++){
for(int j=0;j<=2;j++){
ans = min(ans,Time[num][i][j]);
}
}
if(ans == INF) printf("-1\n");
else printf("%d\n",ans);
}
return 0;
}
相关文章推荐
- 2016北京区域赛E UVAlive 7672 题目:What a Ridiculous Election 带约束条件的BFS
- UVALive 7672 - What a Ridiculous Election (暴力)
- 2016 ACM-ICPC 亚洲区域赛北京站E题 What a Ridiculous Election (BFS预处理)
- hihocoder1426 What a Ridiculous Election(bfs预处理)
- hihocoder1426 What a Ridiculous Election(bfs预处理)
- Regionals 2009 :: Asia - Hsinchu UVALIVE, 4527 Vaccination Centers - 搜索剪枝
- UVALive 4877 记忆搜索
- UVALive3135 UVA1203 POJ2051 ZOJ2212 Argus【优先队列+优先搜索】
- Regionals 2009 >> Asia - Hsinchu UVALIVE, 4528 Schedule Pairs of Jobs - 搜索回溯
- UVALive 3667 Ruler(回溯搜索)
- UVALive-4793 Robots on Ice 搜索加剪枝
- uvalive 5881 map + 搜索记录
- UVALive - 7263 Today Is a Rainy Day (BFS预处理)
- Regionals 2009 >> Asia - Hsinchu UVALIVE, 4529 A Constrained Queen Game - 搜索剪枝
- uvalive 3971 - Assemble(二分搜索 + 贪心)
- ZOJ1256 UVA602 UVALive5474 What Day Is It?【日期】
- uvalive 3971 - Assemble(二分搜索 + 贪心)
- UVALive-7354-Kitchen Measurements(BFS搜索)
- HihoCoder 1426 What a Ridiculous Election (BFS)
- uvalive 3635 - Pie(二分搜索)