hdu5612 Baby Ming and Matrix games (dfs加暴力)
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
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.)
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
Print Impossible if it is impossible to find such an expressions.
3 3 24
1*1
+#*
2*8
1 1 1
1
3 3 3
1*0
/#*
2*6
Possible
Possible
The first sample:1+2*8=24
The third sample:1/2*6=3
#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加暴力)的更多相关文章
- HDU 5612 Baby Ming and Matrix games(DFS)
题目链接 题解:题意为给出一个N*M的矩阵,然后(i∗2,j∗2) (i,j=0,1,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 ...
- 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 ...
- hdu 5612 Baby Ming and Matrix games
Baby Ming and Matrix games 题意: 给一个矩形,两个0~9的数字之间隔一个数学运算符(‘+’,’-‘,’*’,’/’),其中’/’表示分数除,再给一个目标的值,问是否存在从一 ...
- 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 ...
- HDU 5614 Baby Ming and Matrix tree 树链剖分
题意: 给出一棵树,每个顶点上有个\(2 \times 2\)的矩阵,矩阵有两种操作: 顺时针旋转90°,花费是2 将一种矩阵替换为另一种矩阵,花费是10 树上有一种操作,将一条路经上的所有矩阵都变为 ...
- 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 ...
- 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 ( ...
- 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 ...
随机推荐
- parse arguments in bash
There are lots of ways to parse arguments in sh. Getopt is good. Here's a simple script that parses ...
- 获取unity prefab的预览图像
官方的api可以直接获取预览图像,如下所示: Tex=AssetPreview.GetAssetPreview(Object m)as Texture; 但是如果prefab是组合体的话(即一个pre ...
- C语言基础知识【变量】
C 变量1.变量其实只不过是程序可操作的存储区的名称.C 中每个变量都有特定的类型,类型决定了变量存储的大小和布局,该范围内的值都可以存储在内存中,运算符可应用于变量上.变量的名称可以由字母.数字和下 ...
- Java是否存在内存泄露
会的. 原因:长生命周期的对象持有短生命周期对象的引用,导致短生命周期对象不能被回收,由此可能发生内存泄露. 举例参考:http://blog.csdn.net/yakihappy/article/d ...
- static全局变量与普通的全局变量有什么区别?static局部变量和普通局部变量有什么区别?static函数与普通函数有什么区别?
答案:全局变量(外部变量)的说明之前再冠以static 就构成了静态的全局变量.全局变量本身就是静态存储方式,静态全局变量当然也是静态存储方式. 这两者在存储方式上并无不同.这两者的区别虽在于非静态全 ...
- tomcat下发布项目,遇到的问题总结
以前一直是在eclipse下启动tomcat,然后访问web项目.今天脑门一热,就想用tomcat的bin目录下的startup.bat来启动tomcat,虽然tomcat的启动很顺利,但是访问网页的 ...
- Web客户端语言HTML、XHTML和XML相关知识介绍
HTML简介 HTML(Hyper Text Mark-up Language)即超文本标记语言或超文本链接标示语言,是目前网络上应用最为广泛的语言,也是构成网页文档的主要语言.HTML文本是由HTM ...
- Spring JDBC查询返回对象代码跟踪
在封装方法的时候突然发现通过 ResultSetMetaData的getColumnCount()获取到的列明会多一列(ROWSTAT),而且每次的值都是1,目前没有找到相关信息,在国外网站上看到有类 ...
- 学习Sharding JDBC 从入门到出门-02:源码揣测
sjdbc有读写分离的功能,要使用这个功能,在创建数据源对象是要使用类:MasterSlaveDataSource,并且设置主备数据源和数据库名称 这个对象有下面的属性: name:数据库的名称 ma ...
- C#基础知识之三
C#基础知识之三 1. 程序集间的继承:基类必须被声明为public.必须在project中包含对该基类的程序集引用. 2. 对其它程序集引用和添加对using指令的差别:前者是告诉编译器所需的类 ...