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 (Java/Others)
Total Submission(s): 1150 Accepted Submission(s): 298
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.)
T
indicating number of test case.
In the second line there are two odd numbers n,m
and an integer sum(−10
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 3 24
1*1
+#*
2*8
1 1 1
1
3 3 3
1*0
/#*
2*6
Possible
Possible
PossibleHintThe first sample:1+2*8=24
The third sample:1/2*6=3
#include<cstdio>
#include<queue>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int dx[4]={2,-2,0,0};
int dy[4]={0,0,2,-2};
char str[50][50];
bool flag;
int vis[50][50],n,m;
__int64 sum;
bool judge(int x,int y)
{
return x>=0&&x<n&&y>=0&&y<m;
}
void dfs(int x,int y,__int64 a,__int64 b)
{
if(flag) return ;
if(a==b*sum)
{
flag=true;
return;
}
for(int i=0;i<4;i++)
{
int xx=x+dx[i];
int yy=y+dy[i];
if(!judge(xx,yy)||vis[xx][yy]||str[xx][yy]<'0'||str[xx][yy]>'9'||str[xx][yy]=='#')
continue;
__int64 v=str[xx][yy]-'0';
int mx=(x+xx)>>1;
int my=(y+yy)>>1;
__int64 aa=a,bb=b;
if(str[mx][my]=='/'&&v==0) continue;
vis[xx][yy]=1;
if(str[mx][my]=='*'){aa*=v;}
else if(str[mx][my]=='/'){bb*=v;}
else if(str[mx][my]=='+') {aa+=bb*v;}
else {aa-=bb*v;}
dfs(xx,yy,aa,bb);
vis[xx][yy]=0;
}
}
int main()
{
int t;
cin>>t;
while(t--)
{
scanf("%d%d",&n,&m);
scanf("%I64d",&sum);
flag=false;
memset(str,'\0',sizeof(str));
for(int i=0;i<n;i++)
scanf("%s",str[i]);
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(str[i][j]<='9'&&str[i][j]>='0')
{
memset(vis,0,sizeof(vis));
vis[i][j]=1;
__int64 val=str[i][j]-'0';
dfs(i,j,val,1);
if(flag) break;
}
}
if(flag) break;
}
printf(flag?"Possible\n":"Impossible\n");
}
return 0;
}
hdoj--5612--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...)的点处是数字,两个数字之间是符号,其他位置是‘#’号. 但不知道是理解的问题还是题目描述的问题,数据中还 ...
- hdu 5612 Baby Ming and Matrix games
Baby Ming and Matrix games 题意: 给一个矩形,两个0~9的数字之间隔一个数学运算符(‘+’,’-‘,’*’,’/’),其中’/’表示分数除,再给一个目标的值,问是否存在从一 ...
- hdu5612 Baby Ming and Matrix games (dfs加暴力)
Baby Ming and Matrix games Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Ja ...
- 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 ...
- Baby Ming and Matrix games(dfs计算表达式)
Baby Ming and Matrix games Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Ja ...
- 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 ...
随机推荐
- html5——拖拽
基本情况 在HTML5的规范中,我们可以通过为元素增加draggable="true"来设置此元素是否可以进行拖拽操作,其中图片.链接默认是开启的. 拖拽元素 页面中设置了drag ...
- html——标签选择器
交集选择器:标签+类(ID)选择器{属性:值:}.即要满足使用了某个标签,还要满足使用了类(id)选择器. <!DOCTYPE html> <html> <head> ...
- dotnetnuke 获得List 属性
new DotNetNuke.Common.Lists.ListController().GetListEntryInfo("DataType","Text") ...
- Linq处理decimal字段汇总Sum()为NULL
xxxxxxxx.Sum(f => f.jifen).GetValueOrDefault(0)
- Lazarus 1.44升级到1.6 UTF8处理发生变化了
首先这里真的要强调一下,由于Freepascal升级到3.0后,FPC的内部将整个代码处理由AnsiString改为了UTF8编码(RTL with default codepage UTF-8). ...
- ABP生成错误:必须添加对程序集“netstandard”的引用
当前使用ABP版本为:4.6.0 升级vs2017到15.4版本,升级framework到4.7版本 如果Core版本请升级到net core 2
- HDU_5734_数学推公式
题意:给一个向量W={w1,w2……,wn},和一个向量B,B的分量只能为1和-1.求||W-αB||²的最小值. 思路:一来一直在想距离的问题,想怎么改变每一维的值才能使这个向量的长度最小,最后无果 ...
- antiSMASH数据库:微生物次生代谢物合成基因组簇查询和预测
2017年4月28日,核酸研究(Nucleic Acids Research)杂志上,在线公布了一个可搜索微生物次生代谢物合成基因组簇的综合性数据库antiSMASH数据库 4.0版,前3版年均引用2 ...
- js中关于new Object时传参的一些细节分析
1, 参数是一个对象,核心js对象(native ECMAScript object)或宿主对象(host object),那么将直接返回该对象. 其生成的对象构造器仍然是所传参数对象的构造器.这样造 ...
- 最适合初学者的Linux运维学习教程2018版
Linux运维工程师是一个新颖岗位,现在非常吃香,目前从行业的角度分析,随着国内软件行业不断发展壮大,越来越多复杂系统应运而生,为了保证系统稳定运行,必须要有足够多的Linux运维工程师.维护是软件生 ...