题目链接

题解:题意为给出一个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. scip习题(1) scheme和c实现的对比

    习题1.3 定义一个过程,它以三个数为参数,返回其中较大的两个数的平方和. (Define a procedure thats three numbers as argument and return ...

  2. css 开发心得

    1.一个 div内的行内 元素 如何水平对齐 将内部的元素设为 float  ,然后设置 margin-top 对齐,然后 用伪类清楚浮动的影响

  3. 关于float和position

      在div 块级元素中,一般我们的div块都是流式的,如果你设定一个div,接下来的div就会另起行,也就是块级元素的定义 但是一般排版不是这样的,最典型的应该就是这种布局了,那么中间的那三个div ...

  4. CodeForces 707C Pythagorean Triples

    数学,构造. 这题比较有意思,一开始没发现结论写了一个最坏复杂度为$O({10^9})$暴力居然能$AC$,正因为如此,我才发现了规律. 一开始是这么想的: 先假设$n$为直角边,设斜边长度为$c$, ...

  5. 2016 ACM/ICPC Asia Regional Qingdao Online 1005 Balanced Game

    Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...

  6. js便利关联数组 及数组定义方式 分类

    "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv=& ...

  7. css的三大特性

    1.继承性 作用:子元素可以继承父元素的样式text-,font-,line-这些元素开头的都可以继承,以及color属性特殊性: ①. a标签的颜色不能继承,必须对a标签本身进行设置 ②. h标签的 ...

  8. IIS下无法访问.ini后缀文件

    环境: windows server 2003 R2.IIS6 问题: 无法访问网站下的.ini后缀的文件 解决办法: 添加ini文件的MIME映射,让IIS能下载扩展名是ini的文件 添加路径:相应 ...

  9. pythonRedis 订阅功能实现

    两天机器做,host要写订阅主机的ip,客户端发消息,服务端订阅消息 cat redis_connector.py #!/usr/bin/env python__author__ = 'Q.Li'im ...

  10. Redis的windows安装

    第一步:下载Windows下的Redis链接:http://web.kuaipan.cn/n/drive/files/179275650081763322 第二不:打开cmd窗口命令:定位的Redis ...