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的更多相关文章

  1. 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 ...

  2. HDU 5612 Baby Ming and Matrix games(DFS)

    题目链接 题解:题意为给出一个N*M的矩阵,然后(i∗2,j∗2) (i,j=0,1,2...)的点处是数字,两个数字之间是符号,其他位置是‘#’号. 但不知道是理解的问题还是题目描述的问题,数据中还 ...

  3. Baby Ming and Matrix games(dfs计算表达式)

    Baby Ming and Matrix games Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Ja ...

  4. 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 ...

  5. HDU 5614 Baby Ming and Matrix tree 树链剖分

    题意: 给出一棵树,每个顶点上有个\(2 \times 2\)的矩阵,矩阵有两种操作: 顺时针旋转90°,花费是2 将一种矩阵替换为另一种矩阵,花费是10 树上有一种操作,将一条路经上的所有矩阵都变为 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. HDU 5613 Baby Ming and Binary image

    因为第一行和最后一行都是0,我们只需枚举最左边或最右边一列的01情况,即可得到整张表 然后再检验表是否符合要求 #include<cstdio> #include<cstring&g ...

随机推荐

  1. 仰视源代码,实现memcpy

    C++实现内存的复制 通常我们使用深复制就是通过内存复制实现的,可是对象的复制涉及到基类派生类及其相关类的问题.这里不讨论. 目的为了可以明确内存复制的底层实现. void* memcpy(void* ...

  2. 发送email给列表中的邮箱--python

    #!/usr/bin/python # -*- coding: utf-8 -*- # from email.Header import Header from email.MIMEText impo ...

  3. [Angular 2] Handle Reactive Async opreations in Service

    When you use ngrx/store and you want to fire a service request. When it sucessfully return the respo ...

  4. linux使用过程中遇到的问题和解决方法

      测试过程中,出现以下错误,导致远程ssh连接不上,最终导致测试结果失败.系统日志如下: Sep 1 03:15:03 node3 avahi-daemon[5834]: Invalid respo ...

  5. Java基础知识强化之集合框架笔记71:模拟斗地主洗牌和发牌并对牌进行排序的案例

    1. 模拟斗地主洗牌和发牌并对牌进行排序的原理图解: 2. 代码实现: 思路: • 创建一个HashMap集合 • 创建一个ArrayList集合 • 创建花色数组和点数数组 • 从0开始往HashM ...

  6. 使用Spring Boot快速构建应用

    http://www.infoq.com/cn/news/2014/01/spring-boot/ 随着Spring 4新版本的发布,Spring Boot这个新的子项目得到了广泛的关注,因为不管是S ...

  7. 安装Sass的一些注意事项

    文章的内容都来自网络: 1. RubyGems 镜像 2.  compass watch,我们的Sass种如果出现了中文, 就会出现编译错误,我们需要: 找到ruby的安装目录,里面也有sass模块, ...

  8. MySQL数据库分表的3种方法

    原文地址:MySQL数据库分表的3种方法作者:dreamboycx 一,先说一下为什么要分表 当一张的数据达到几百万时,你查询一次所花的时间会变多,如果有联合查询的话,我想有可能会死在那儿了.分表的目 ...

  9. JQ 如何设置单选按钮问题

    <input type="radio" name="db_12" value="2" checked="checked/&g ...

  10. Lucene.net常用功能说明

    Lucene.net是一个.net下的全文检索类库.配置简单,功能丰富,比较成熟.我在项目中用Lucene.net有一段时间了,这里我把常用一些功能写出来,与大家一起分享. Lucene.net用的是 ...