杭电3699A hard Aoshu Problem DFS 搜索
来源:互联网 发布:新页进销存软件免费 编辑:程序博客网 时间:2024/06/03 22:52
A hard Aoshu Problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 62768/32768 K (Java/Others)Total Submission(s): 1521 Accepted Submission(s): 800
ABBDE __ ABCCC = BDBDE
In the equation above, a letter stands for a digit(0 – 9), and different letters stands for different digits. You can fill the blank with ‘+’, ‘-‘ , ‘×’ or ‘÷’.
How to make the equation right? Here is a solution:
12245 + 12000 = 24245
In that solution, A = 1, B = 2, C = 0, D = 4, E = 5, and ‘+’ is filled in the blank.
When I was a kid, finding a solution is OK. But now, my daughter’s teacher tells her to find all solutions. That’s terrible. I doubt whether her teacher really knows how many solutions are there. So please write a program for me to solve this kind of problems.
Each test case is a line which is in the format below:
s1 s2 s3
s1, s2 and s3 are all strings which are made up of capital letters. Those capital letters only include ‘A’,’B’,’C’,’D’ and ‘E’, so forget about ‘F’ to ‘Z’. The length of s1,s2 or s3 is no more than 8.
When you put a ‘=’ between s2 and s3, and put a operator( ‘+’,’-‘, ‘×’ or ‘÷’.) between s1 and s2, and replace every capital letter with a digit, you get a equation.
You should figure out the number of solutions making the equation right.
Please note that same letters must be replaced by same digits, and different letters must be replaced by different digits. If a number in the equation is more than one digit, it must not have leading zero.
2A A ABCD BCD B
572
#include<stdio.h>#include<string.h>#include<map>#include<algorithm>using namespace std;char s1[12], s2[12], s3[12];int a, b, c;int len1, len2, len3;int ans, num;bool vis[12];map<char, int> mm; // 表示映射,int表示 字母char是第几个加入map里面的,从1开始(去除重复)int d[13]; //记录搜索后各字母所对应的值。void dfs(int dep){ int i, j; if(dep == num) { a = b = c = 0; for(i = 0; i < len1; i++) a = a * 10 + d[mm[s1[i]]]; for(i = 0; i < len2; i++) b = b * 10 + d[mm[s2[i]]]; for(i = 0; i < len3; i++) c = c * 10 + d[mm[s3[i]]]; //printf("%d %d %d\n", a, b, c); if(a + b == c) ans++; if(a == b * c && b != 0) ans++; if(a - b == c) ans++; if(a * b == c) ans++; return ; } for(i = 0; i <= 9; i++) { if(vis[i]) continue; if(dep == mm[s1[0]] && i == 0 && len1 >= 2) continue; // 搜到最高位数字 每个数前面不能是0,去掉这些情况 if(dep == mm[s2[0]] && i == 0 && len2 >= 2) continue; if(dep == mm[s3[0]] && i == 0 && len3 >= 2) continue; vis[i] = 1; d[dep] = i; dfs(dep+1); vis[i] = 0; }}int main(){ int i, j, cas; scanf("%d", &cas); while(cas--) { num = 1; ans = 0; scanf("%s%s%s", s1, s2, s3); len1 = strlen(s1); len2 = strlen(s2); len3 = strlen(s3); mm.clear(); for(i = 0; i < len1; i++) if(mm[s1[i]] == 0) mm[s1[i]] = num++; for(i = 0; i < len2; i++) if(mm[s2[i]] == 0) mm[s2[i]] = num++; for(i = 0; i < len3; i++) if(mm[s3[i]] == 0) mm[s3[i]] = num++; memset(vis, 0, sizeof(vis)); dfs(1); printf("%d\n", ans); } return 0;}
- 杭电3699A hard Aoshu Problem DFS 搜索
- 杭电4403 A very hard Aoshu problem DFS
- 暴力dfs 3699 A hard Aoshu Problem
- HDU 3699(J) ——A hard Aoshu Problem(DFS暴力搜索)
- HDU 3699 A hard Aoshu Problem (暴力搜索)
- HDU 3699 A hard Aoshu Problem
- HDU 3699 A hard Aoshu Problem
- HDU 4430 A very hard Aoshu problem DFS 数学式子
- HDU 4403 A very hard Aoshu problem (DFS)
- HDU 4403 A very hard Aoshu problem(DFS)
- HDU 4403 A very hard Aoshu problem(DFS)
- HDU 4403 A very hard Aoshu problem (DFS暴力)
- HDU 4403 A very hard Aoshu problem(DFS+暴力)
- HDU 4403 A very hard Aoshu problem(双向dfs)
- HDOJ 题目A very hard Aoshu problem(暴力,DFS)
- HDU 4403 A very hard Aoshu problem (DFS)
- hdu 4403 A very hard Aoshu problem(dfs)
- HDU 4403 A very hard Aoshu problem【DFS】
- LINUX通过命令行操作MYSQL
- Java实现二叉树的基本运算
- Websocket原理
- 王歌:随时随地练习腹部呼吸减肥方法!
- 动态、静态编译以及MD、MDd、MT、MTd编译
- 杭电3699A hard Aoshu Problem DFS 搜索
- UML类图关系(泛化 、继承、实现、依赖、关联、聚合、组合)
- ACM-icpc---bfs求最短路
- mybatis系列四:mybatis 动态sql语句
- mysql组织机构树迭代查询
- (13.1.3.5)PMBOK之三:十大知识领域之质量管理
- c语言总结(二)
- Windows下Vue开发环境搭建及相关问题
- JavaScript初级玩法(4)—两数交换的多种方法