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 ...
随机推荐
- unity 旋转两种方法
transform.Rotate(new Vector3(0, 10, 10)*speed*Time.deltaTime); // 物体绕x轴.y轴.z轴旋转 transform.RotateArou ...
- CloseableHttpClient 在使用过程中遇到的问题
代码是前辈写的,在对代码进行压测的时候遇到了个问题,最大线程是 不能超过setDefaultMaxPerRoute设置的数字,一点超过 就会死掉.这里会报错 connection pool shut ...
- jQuery——自定义动画
动画方法:animate(json,1000, function (){}) 参数说明:json代表属性设置,1000是动画时间,最后一个是回调函数,其中动画时间可选 属性支持:http://www. ...
- python读取单个文件操作
python读取单个文件,参考<笨方法学python>的第15节. 运行方式是采用:python python文件名 要读取的文件名 代码中 script, filename = argv ...
- Python开发工具搭建-Pycharm
PyCharm2017. 3.X专业版 安装使用. 注册码激活 本文以 Windows系统 为例: 1.开发工具获取及下载 Anaconda(Python 的集成工具 ) 下载地址: https:// ...
- 宏基因组扩增子图表解读2散点图:组间整体差异分析(Beta多样性)
散点图 数据点在直角坐标系平面上的分布图.在宏基因组领域,散点图常用于展示样品组间的Beta多样性,常用的分析方法有主成分分析(PCA),主坐标轴分析(PCoA/MDS)和限制条件的主坐标轴分析(CP ...
- 实现动画之CSS与JavaScript对比
曾经某个时期,大多数开发者使用 jQuery 给浏览器中的元素添加动画.让这个淡化,让那个扩大,很简单.随着互动的项目越来越复杂,移动设备的大量增加,表现性能变得越来越重要.Flash 被抛弃,有天赋 ...
- spine骨骼动画组件使用详解
1. spine骨骼动画工具 骨骼动画: 把动画打散, 通过工具,调骨骼的运动等来形成动画spine是一个非常流行的2D骨骼动画制作工具spine 动画美术人员导出3个文件: (1) .png文 ...
- 踩过的坑:__file__、__package__和__name__
不说废话,直接上示例结构图 Path.py内容如下: import os path1 = os.path.dirname(os.path.abspath(__file__)) path2 = os.p ...
- Python 查看关键字
关键字 import keyword print(keyword.kwlist)