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. UVa 10651 Pebble Solitaire(DP 记忆化搜索)

    Pebble Solitaire Pebble solitaire is an interesting game. This is a game where you are given a board ...

  2. iOS 生命周期 -init、viewDidLoad、viewWillAppear、viewDidAppear、viewWillDisappear、viewDidDisappear 区别和用途

    iOS视图控制对象生命周期-init.viewDidLoad.viewWillAppear.viewDidAppear.viewWillDisappear.viewDidDisappear的区别及用途 ...

  3. web安全之SQL注入---第三章 如何寻找sql注入?

    借助逻辑推理1.识别web应用中所有输入点2.了解哪些类型的请求会触发异常3.检测服务器响应中的异常 总结: 输入点无非就是:地址栏.和输入框 输入康输入一些非法字符,导致后台的sql语句错误,

  4. eclipse里面用svn关联项目

    eclipse里面共享项目经常会用到svn或者git插件 关联项目的步骤如下: 如果 点击finish会遇到卡住问题的话,不要着急,我们需要设置svn的client设置: 如果设置了之后还是很卡的话, ...

  5. Java编程中的一些常见问题汇总

    转载自  http://macrochen.iteye.com/blog/1393502 每天在写Java程序,其实里面有一些细节大家可能没怎么注意,这不,有人总结了一个我们编程中常见的问题.虽然一般 ...

  6. Linux中假设系统丢失了ls命令

    假设系统丢失了ls命令,如何修复: 1.查询ls命令属于那个rpm包[root@localhost ~]# rpm -qf /bin/lscoreutils-8.4-43.el6.x86_64 2.挂 ...

  7. IOS - 执行时 (多态)

    一 多态概述          多态指同一操作作用于不同的对象.能够有不同的解释.产生不同的执行结果.它是面向对象程序设计(OOP)的一个重要特征,动态类型能使程序直到执行时才确定对象的所属类.其详细 ...

  8. (转)linux访问windows共享文件夹的两种方法

    有时需要在linux下访问window的共享文件,可以使用mount挂载或使用samba连接. 1,mount挂载 $ mkdir windows 将共享文件夹挂载到windows文件夹: mount ...

  9. linux多个分区合并为一个分区

    备份# rsync -avP -e ssh /data xxx卸载# umount /data /data?设置分区类型为8e# fdisk /dev/sdb 创建PV# pvcreate /dev/ ...

  10. sql语句 字段的赋值

    将同一个表中的一个字段2的所有值赋值给另一个字段1 UPDATE 表名 SET 字段1 = 字段2 也可以把字段所有的值赋为null UPDATE 表名 SET 字段1 = null