题目链接

题解:题意为给出一个N*M的矩阵,然后(i∗2,j∗2) (i,j=0,1,2...)的点处是数字,两个数字之间是符号,其他位置是‘#’号。
但不知道是理解的问题还是题目描述的问题,数据中还有类似1#1这种数据存在,因此WA了4次,加上了一句代码后,马上AC了,该行代码在下文以斜粗体标出。
此外,因为里面有除法,会有一定误差,所以用这句来判断:fabs(tar-nnum)<=1e-8,而不是tar==nnum。
 
#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <cstring>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <map>
#define ms(a) memset(a,0,sizeof(a))
using namespace std;
#define LOCAL
char mp[][];
int n,m;
double tar;
bool flag;
int dir[][]= {{,},{,-},{,},{-,}};
double cal(double n,double m,char c)
{
if(c=='+')return n+m;
if(c=='-')return n-m;
if(c=='*')return n*m;
if(c=='/')return n/m;
return ;
}
void dfs(int x,int y,double num)
{
if(flag)return;
for(int i=; i<; i++)
{
int nx=x+dir[i][],ny=y+dir[i][];
if(mp[nx][ny]!='#'&&(nx>=&&nx<n&&ny>=&&ny<m))
{
char ch=mp[nx][ny];
mp[nx][ny]='#';
if(mp[x+dir[i][]/][y+dir[i][]/]=='#')return;
double nnum=cal(num,ch-'',mp[x+dir[i][]/][y+dir[i][]/]);
if(fabs(tar-nnum)<=1e-)
{
flag=true;
return;
}
dfs(nx,ny,nnum);
mp[nx][ny]=ch;
}
}
return;
}
int main()
{
#ifdef LOCAL
freopen("in.txt", "r", stdin);
#endif // LOCAL
//Start
int N;
cin>>N;
while(N--)
{
cin>>n>>m>>tar;
ms(mp);
flag=false;
for(int i=; i<n; i++)
{
cin>>mp[i];
}
for(int i=; i<n; i++)
{
for(int j=; j<m; j++)
{
if(isdigit(mp[i][j]))
{
char sav=mp[i][j];
if((sav-'')==tar)
{
flag=true;
break;
}
mp[i][j]='#';
dfs(i,j,sav-'');
mp[i][j]=sav;
}
if(flag)break;
}
if(flag)break;
}
if(flag)printf("Possible\n");
else printf("Impossible\n");
}
return ;
}

HDU 5612 Baby Ming and Matrix games(DFS)的更多相关文章

  1. hdu 5612 Baby Ming and Matrix games

    Baby Ming and Matrix games 题意: 给一个矩形,两个0~9的数字之间隔一个数学运算符(‘+’,’-‘,’*’,’/’),其中’/’表示分数除,再给一个目标的值,问是否存在从一 ...

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

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

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

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

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

  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. 主成分分析(Principal components analysis)-最大方差解释

    原文:http://www.cnblogs.com/jerrylead/archive/2011/04/18/2020209.html 在这一篇之前的内容是<Factor Analysis> ...

  2. Linux安装Firefly

    1.安装一些必要的东东 yum install -y gcc zlib zlib-devel lrzsz openssl openssl-devel python-devel mysql-devel ...

  3. VsVim的快捷键使用

    .插入命令(可配合数字使用) i 在当前位置前插入 I 在当前行首插入 a 在当前位置后插入 A 在当前行尾插入 o 在当前行之后插入一行 O 在当前行之前插入一行 ni/a/o/I/A/O<E ...

  4. Hadoop优先级调度

    当同时在集群中运行多个作业时,默认情况下,Hadoop将提交的作业放入一个FIFO,一个作业结束后,Hadoop就启动下一个作业. 当一个运行时间长但是优先级较低的作业先于运行时间短而优先级较高的作业 ...

  5. 利用Runtime给UITextView添加占位符(新方法)

     以前一直使用自定义UITextView通过通知中心来自定义placeHolder,最近看到这个方法,感觉更好 UITextView *textView = [[UITextView alloc]in ...

  6. 解决IE兼容总汇【转】

    转载声明: 藏羚羊 2014年04月16日 于 前端开拓者 发表 本文固定链接: http://www.frontopen.com/2552.html 1. <meta http-equiv=“ ...

  7. Java的“影子克隆”和“深度克隆”

    今天来学习学习java对象的克隆,在写代码的时候,有时候我们会这样写:对象1=对象2,也就是把对象2赋值给对象1了,但是这样做有个问题,就是如果我们修改了对象2的属性值,对象1的相同属性值也被修改了, ...

  8. ios发布笔录

    需要一张1024x1024的icon 发布尺寸4.7英寸  1334x7505.5英寸 2208-12424英寸  1136-6403.5英寸 960-640ipad  2048x1536 视频 ip ...

  9. foreach绑定

    目的 foreach可以将一个数组中的实体循环的进行绑定.这在将一个list显示成table时非常有用. 假设数组是observable的,当在绑定后做了add, remove,或者重新排序后,绑定会 ...

  10. Flex 国际化(中英语言适配)

    原文地址:http://www.cnblogs.com/meteoric_cry/archive/2011/01/13/1934404.html(由于此贴时间久远,已做微调) 1.新建Flex Pro ...