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. java实体类如果不重写toString方法,会如何?

    先认识一下Object Object 类的 toString 方法 返回一个字符串,该字符串由类名(对象是该类的一个实例).at 标记符“@”和此对象哈希码的无符号十六进制表示组成.换句话说,该方法返 ...

  2. 录音整理文字工具otranscribe简介

    网址: http://otranscribe.com/ 首先载入音频文件,支持 mp3, ogg, webm, wav (HTML5 无需将文件上传至服务器,可保护隐私),然后就可以边听边整理了. 通 ...

  3. C语言基础知识【运算符】

    C 运算符1.运算符是一种告诉编译器执行特定的数学或逻辑操作的符号.C 语言内置了丰富的运算符,并提供了以下类型的运算符:算术运算符关系运算符逻辑运算符位运算符赋值运算符杂项运算符2.杂项运算符 ↦ ...

  4. iOS base64加密解密

    本文转载至 http://jingyan.baidu.com/article/93f9803fff45c9e0e46f5596.html 从参考资料的地址中下载GTMBase64.zip库文件包,并解 ...

  5. Java 学习 day08

    01-面向对象(多态-概念) package myFirstCode; /* 多态:可以理解为事务存在的多种体现形态. 人:男人,女人 动物:猫,狗 猫 x = new 猫(); 动物 x = new ...

  6. 【BZOJ4930】棋盘 拆边费用流

    [BZOJ4930]棋盘 Description 给定一个n×n的棋盘,棋盘上每个位置要么为空要么为障碍.定义棋盘上两个位置(x,y),(u,v)能互相攻击当前仅 当满足以下两个条件: 1:x=u或y ...

  7. org.apache.poi3.1.7 Excle并发批量导入导出

    org.apache.poi3.1.7 升级,需要修改设置方式: 1.org.apache.poi3.1.4 的设置单元格: XSSFCellStyle cellStyle = wb.createCe ...

  8. WIn10远程:mstsc:出现身份验证错误,要求的函数不支持, 这可能是由于CredSSP加密Oracle修正

    a.单击 开始 > 运行,输入 regedit,单击 确定. b.定位到 HKLM\Software\Microsoft\Windows\CurrentVersion\Policies\Syst ...

  9. centos7 mysql允许远程连接设置

    Mysql为了安全性,在默认情况下用户只允许在本地登录,可是在有此情况下,还是需要使用用户进行远程连接,因此为了使其可以远程需要进行如下操作: 一.允许root用户在任何地方进行远程登录,并具有所有库 ...

  10. 蓝屏代码stop:0X000000EA(0X85E286B8,0X8635F210,0XF7A53CBC,0X00000001)

    你这是显卡驱动问题,我把蓝屏代码都给你,以后在出现蓝屏自己看看行了. 1.0x0000000A:IRQL_NOT_LESS_OR_EQUAL ◆错误分析:主要是由问题的驱动程序.有缺陷或不兼容的硬件与 ...