Baby Ming and Matrix games

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 849    Accepted Submission(s): 211

Problem Description
These few days, Baby Ming is addicted to playing a matrix game.

Given a n∗m matrix, the character in the matrix(i∗2,j∗2) (i,j=0,1,2...) are the numbers between 0−9. There are an arithmetic sign (‘+’, ‘-‘, ‘∗’, ‘/’) between every two adjacent numbers, other places in the matrix fill with ‘#’.

The question is whether you can find an expressions from the matrix, in order to make the result of the expressions equal to the given integer sum. (Expressions are calculated according to the order from left to right)

Get expressions by the following way: select a number as a starting point, and then selecting an adjacent digital X to make the expressions, and then, selecting the location of X for the next starting point. (The number in same place can’t be used twice.)

 
Input
In the first line contains a single positive integer T, indicating number of test case.

In the second line there are two odd numbers n,m, and an integer sum(−1018<sum<1018, divisor 0 is not legitimate, division rules see example)

In the next n lines, each line input m characters, indicating the matrix. (The number of numbers in the matrix is less than 15)

1≤T≤1000

 
Output
Print Possible if it is possible to find such an expressions.

Print Impossible if it is impossible to find such an expressions.

 
Sample Input
3
3 3 24
1*1
+#*
2*8
1 1 1
1
3 3 3
1*0
/#*
2*6
 
Sample Output
Possible
Possible
Possible

Hint

The first sample:1+2*8=24
The third sample:1/2*6=3

题意:在这个矩阵内是否可以找到一个表达式的值等于sum;
思路:dfs找到所有的表达式,暴力一发;
AC代码:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
double
sum;
int
t,n,m,flag;
int
vis[][],dir[][]={-,,,,,,,-};
double
num[][];
char
s[][];
int
dfs(int x,int y,double ans)
{

vis[x][y]=;
if
(fabs(ans-sum)<=0.000000001)flag=;
for
(int i=;i<;i++)
{

int
fx=x+dir[i][],fy=y+dir[i][];
int
px=x+dir[i][]/,py=y+dir[i][]/;
if
(fx>=&&fx<=n&&fy<=m&&fy>=&&vis[fx][fy]==&&s[fx][fy]!='#')
{

if
(s[px][py]=='+')
dfs(fx,fy,ans+num[fx][fy]);
else if
(s[px][py]=='*')
dfs(fx,fy,ans*num[fx][fy]);
else if
(s[px][py]=='-')
dfs(fx,fy,ans-num[fx][fy]);
else if
(s[px][py]=='/'&&num[fx][fy]!=)
dfs(fx,fy,ans/num[fx][fy]);
}
}

vis[x][y]=;
return
;
}

int
main()
{

scanf("%d",&t);
while
(t--)
{

flag=;
memset(vis,,sizeof(vis));
memset(num,-,sizeof(num));
scanf("%d%d%lf",&n,&m,&sum);
for
(int i=;i<=n;i++)
{

scanf("%s",s[i]+);
}

for
(int i=;i<=n;i++)
{

for
(int j=;j<=m;j++)
{

if
(s[i][j]>='0'&&s[i][j]<='9')
{

num[i][j]=s[i][j]-'0';
}
}
}

for
(int i=;i<=n;i++)
{

for
(int j=;j<=m;j++)
{

if
(s[i][j]>='0'&&s[i][j]<='9')
{

dfs(i,j,num[i][j]);
}
}
}

if
(flag)printf("Possible\n");
else
printf("Impossible\n");
}

return
;
}

hdu5612 Baby Ming and Matrix games (dfs加暴力)的更多相关文章

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

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

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

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

  4. hdu 5612 Baby Ming and Matrix games

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

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

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

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

  7. Learning in Two-Player Matrix Games

    3.2 Nash Equilibria in Two-Player Matrix Games For a two-player matrix game, we can set up a matrix ...

  8. BestCoder Round #69 (div.2) Baby Ming and Weight lifting(hdu 5610)

    Baby Ming and Weight lifting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K ( ...

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

随机推荐

  1. 在ListView的GroupItem头中显示每列的Summary

    问题描述 WPF自带的ListView和DataGrid控,都提供了数据分组的支持,并可以对分组的Header进行自定义.但是,如果想在每个分组的Header中,显示出本分组的"小计&quo ...

  2. XSD文件详解(二)

    <?xml version="1.0" encoding="gb2312"?> <studentlist>   <student  ...

  3. OpenCV玩耍(一)批量resize一个文件夹里的所有图像

    鉴于用caffe做实验的时候,里面牵扯到一个问题是必须将训练集和测试集都转成256*256的图像,而官网给出的代码又不会用,所以我用opencv转了.其实opencv只转一幅图会很简单,关键在于“批量 ...

  4. Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [32,176] milliseco

    有一次,我启动tomcat时,居然花费了33秒.我不理解为什么一个新的tomcat,需要这么久, 网上查找后,找到了一个解决方法. # vim /usr/local/tomcat/bin/catali ...

  5. Django 之 ModelForm 组件

    Django的model form组件 扩展:Django 之Form组件 首先我们要知道 Model 和 Form 分别时干什么的 Model  生成表数据 Form  对表单.字段进行校验 Dja ...

  6. header函数使用

    header('HTTP/1.1 200 OK'); //设置一个404头: header('HTTP/1.1 404 Not Found'); //设置地址被永久的重定向 header('HTTP/ ...

  7. SAP HR 复制PA30的人员

    [转自http://www.512test.com/home/space.php?uid=19&do=blog&id=2381] 很多顾问测试HR的程序时都为录入人员头痛,下面的程序提 ...

  8. 从springmvc启动日志学习

    javaee标准中,tomcat等web容器启动时走web.xml 先将各种contex-param 放到servletcontxt中变成parameter,然后开始启动容器,容器对外提供了liste ...

  9. vmware 下的三种网络模式

    VMWare提供三种工作模式桥接(bridge).NAT(网络地址转换)和host-only(主机模式). 桥接模式 在桥接模式下,VMWare虚拟出来的操作系统就像是局域网中的一台独立的主机(主机和 ...

  10. linux基础part4

    linux基础 一.系统监控命令 1.top命令: a.如图显示使用top命令查看系统的当前运行的情况.如图对top命令执行的结果做了简单的图解,下面针对每一项做详细的解释. b.第一行显示的内容依次 ...