hdu 5612 Baby Ming and Matrix games
题意: 给一个矩形,两个0~9的数字之间隔一个数学运算符(‘+’,’-‘,’*’,’/’),其中’/’表示分数除,再给一个目标的值,问是否存在从一个数字出发,以数字之间的运算符为运算,得到这个目标值;(每个数字只能用一次,其实说白了就是dfs..);可以则输出(Impossible),否则输出(Possible);
思路:坑点就是里面本来全是整数,但是一个除法运算却是分数形式,开始使用了很保险的分数保存,来避免误差的。但是无情WA了很多次。。突然发现写成了(ImPossible)…醉了,然后以为现在肯定要A了,但是还是WA了。。。(有兴趣的帮我找找bug哈!真心感谢)之后重写了一个double类型的,无奈啊(太水了),下面是A和WA两份代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string.h>
#include<algorithm>
#include<map>
#include<queue>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<time.h>
using namespace std;
#define rep(i,n) for(int i = 1;i <= n;i++)
#define MS0(a) memset(a,0,sizeof(a))
#define esp 1e-6
int n,m,vis[][],f;
double sum;
char s[][];
int dir[][] = {{,,,-},{,,-,}};
double cal(double val,double v,char op)
{
if(op == '+') return val + v;
else if(op == '-') return val - v;
else if(op == '*') return val * v;
return val/v;
}
void dfs(int i,int j,double val)
{
if(fabs(val - sum) < esp) f = ;
for(int k = ;k < && f;k++){
int x = i + *dir[][k] , y = j + *dir[][k];
char op = s[i + dir[][k]][j + dir[][k]];
if(x < || x > n || y < || y > m || vis[x][y]) continue;
int v = s[x][y] - '';
if(op == '/' && v == ) continue;
vis[x][y] = ;
dfs(x,y,cal(val,v,op));
vis[x][y] = ;
}
}
int main()
{
int T,i,j;
cin>>T;
while(T--){
f= ;MS0(vis);
scanf("%d%d%lf",&n,&m,&sum);
rep(i,n) scanf("%s",s[i] + );
for(i = ;i <= n && f;i += )
for(j = ;j <= m && f;j += ){
vis[i][j] = ;
dfs(i,j,s[i][j] - '');
vis[i][j] = ;
}
puts(f?"Impossible":"Possible");
}
return ;
}
Accpeted code
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string.h>
#include<algorithm>
#include<map>
#include<queue>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<time.h>
using namespace std;
typedef long long ll;
#define MS0(a) memset(a,0,sizeof(a))
ll cal(ll a,ll b,char op)
{
if(op == '+') return a + b;
if(op =='-') return a - b;
if(op == '*') return a * b;
}
int dir[][] = {{,,,-},{,,-,}};
char s[][];
ll sum,f;
int n,m,vis[][];
void dfs(int i,int j,ll ele,ll den)
{
if(ele%den == && ele / den == sum) f = ;
ll e = ele, d = den;
for(int k = ;k < && f;k++){
//system("pause");
int x = i + *dir[][k],y = j + *dir[][k];
char op = s[i+dir[][k]][j+dir[][k]];
if(x < || x > n || y < || y > m||vis[x][y]) continue;
ele = e,den = d;
int val = (s[x][y]-'');
if(op =='/'){
if(val == ) continue;
if(ele % val) den *= val;
else ele /= val;
}
else ele = cal(ele,val,op);
if(ele % den == ) ele /= den,den = ;
vis[x][y] = ;
dfs(x,y,ele,den);
vis[x][y] = ;
}
}
int main()
{
int T,i,j;
cin>>T;
while(T--){
MS0(vis);
scanf("%d%d%I64d",&n,&m,&sum);
for(i = ;i <= n;i++)
scanf("%s",s[i] + );
f = ;
for(i = ;i <= n && f;i += )
for(j = ;j <= m && f;j += ){
vis[i][j] = ;
dfs(i,j,(ll)(s[i][j] - ''),);
vis[i][j] = ;
}
puts(f?"Impossible":"Possible");
}
return ;
}
WA Code
hdu 5612 Baby Ming and Matrix games的更多相关文章
- hdu 5612 Baby Ming and Matrix games(dfs暴力)
Problem Description These few days, Baby Ming is addicted to playing a matrix game. Given a n∗m matr ...
- HDU 5612 Baby Ming and Matrix games(DFS)
题目链接 题解:题意为给出一个N*M的矩阵,然后(i∗2,j∗2) (i,j=0,1,2...)的点处是数字,两个数字之间是符号,其他位置是‘#’号. 但不知道是理解的问题还是题目描述的问题,数据中还 ...
- Baby Ming and Matrix games(dfs计算表达式)
Baby Ming and Matrix games Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Ja ...
- hdu5612 Baby Ming and Matrix games (dfs加暴力)
Baby Ming and Matrix games Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Ja ...
- HDU 5614 Baby Ming and Matrix tree 树链剖分
题意: 给出一棵树,每个顶点上有个\(2 \times 2\)的矩阵,矩阵有两种操作: 顺时针旋转90°,花费是2 将一种矩阵替换为另一种矩阵,花费是10 树上有一种操作,将一条路经上的所有矩阵都变为 ...
- hdoj--5612--Baby Ming and Matrix games(dfs)
Baby Ming and Matrix games Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K ...
- hdu 5611 Baby Ming and phone number(模拟)
Problem Description Baby Ming collected lots of cell phone numbers, and he wants to sell them for mo ...
- hdu 5610 Baby Ming and Weight lifting
Problem Description Baby Ming is fond of weight lifting. He has a barbell pole(the weight of which c ...
- HDU 5613 Baby Ming and Binary image
因为第一行和最后一行都是0,我们只需枚举最左边或最右边一列的01情况,即可得到整张表 然后再检验表是否符合要求 #include<cstdio> #include<cstring&g ...
随机推荐
- Vim 快捷键整理
一.移动光标 1.左移h.右移l.下移j.上移k 2.向下翻页ctrl + f,向上翻页ctrl + b 3.向下翻半页ctrl + d,向上翻半页ctrl + u 4.移动到行尾$,移动到行首0(数 ...
- 场景类(CCSence)
场景与流程控制 在图2-1中,每一个节点中显示的内容相对不变.通常,我们把这些内容相对不变的游戏元素集合称作场景(scene),把游戏在场景之间切换的过程叫做流程控制(flow control). 在 ...
- 设置background属性使用selector的时候内置?attr报错的解决方案
当我们设置background属性的时候可以设置background="@color/black" 也可以设置 background="@drawable/selecto ...
- 2016iweb峰会参会总结
2016年8月27日去国家会议中心参加iweb峰会. 8点半开始签到入场,8点20分排队签到的人已经排到另一个门口,人超级多啊. 9点一如既往的由h5女神娜姐开场. 上午场 基本是各公司的大佬们介绍各 ...
- Eclipse错误
1.java compiler level does not match the version of the installed java project facet 解决:http://blog. ...
- redis resque消息队列
Resque 目前正在学习使用resque .resque-scheduler来发布异步任务和定时任务,为了方便以后查阅,所以记录一下. resque和resque-scheduler其优点在于功能比 ...
- css placeholder 颜色设置
因为每个浏览器的CSS选择器都有所差异,所以需要针对每个浏览器做单独的设定(可以在冒号前面写input和textarea). ::-webkit-input-placeholder { /* WebK ...
- java中判断Object对象类型
记录一下 Object param = params.get(i); if (param instanceof Integer) { int value = ((Integer) param).int ...
- 关于dialog的一点知识
一般我们比较常用的就是AlertDialog,这个一般也不直接构造,而是用系统提供的builder构造器去构造一个dialog. AlertDialog.Builder builder = new A ...
- WPF-TxtBox控件利用KeyDown来控制键盘输入
private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e) { TextBox txt = ...