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 ...
随机推荐
- ViewPage + Fragment 防止Fragment 重复加载问题
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanc ...
- 大数据hadoop之zookeeper
一.ZooKeeper 的实现 1.1 ZooKeeper处理单点故障 我们知道可以通过ZooKeeper对分布式系统进行Master选举,来解决分布式系统的单点故障,如图所示. 图 1.1 ZooK ...
- linux下apache php配置redis
1.安装redis 第一步: 下载:https://github.com/nicolasff/phpredis/archive/2.2.4.tar.gz 上传phpredis-2.2.4.tar.gz ...
- Mockito when(...).thenReturn(...)和doReturn(...).when(...)的区别
在Mockito中打桩(即stub)有两种方法when(...).thenReturn(...)和doReturn(...).when(...).这两个方法在大部分情况下都是可以相互替换的,但是在使用 ...
- obj-c学习笔记
本文转载至 http://blog.csdn.net/c395565746c/article/details/7573793 当对象经过在dealloc方法处理时,该对象就已经处于已销毁状态,其它 ...
- MySql 查询列中包含数据库的关键字
MySQL查询列表中包含数据的关键字的处理办法是用``把关键字包起来(tab键上面的字符)
- python多进程-----multiprocessing包
multiprocessing并非是python的一个模块,而是python中多进程管理的一个包,在学习的时候可以与threading这个模块作类比,正如我们在上一篇转载的文章中所提,python的多 ...
- 基于SqlDependency的Asp.net数据缓存
首先,确保目标数据库的is_broker_enabled已经enabled. SELECT name, is_broker_enabled FROM sys.databases 如果不是enabled ...
- 【BZOJ3784】树上的路径 点分治序+ST表
[BZOJ3784]树上的路径 Description 给定一个N个结点的树,结点用正整数1..N编号.每条边有一个正整数权值.用d(a,b)表示从结点a到结点b路边上经过边的权值.其中要求a< ...
- SpringMVC拦截器实现登录认证
项目结构如图: 需要的jar:有springMVC配置需要的jar和jstl需要的jar SpringMVC包的作用说明: aopalliance.jar:这个包是AOP联盟的API包,里面包含了针对 ...