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

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
 
Source
 
Recommend
hujie   |   We have carefully selected several similar problems for you:  5639 5638 5637 5636 5635 

传递的参数都是整数,如果遇到比较大的分母可能表达式的值为0,所以传递的b表示分母,遇到除法就把数乘到分母上,加法或者减法就乘以分母然后加减到分子上,最后判断的时候看是否等于sum*b
#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)的更多相关文章

  1. HDU 5612 Baby Ming and Matrix games(DFS)

    题目链接 题解:题意为给出一个N*M的矩阵,然后(i∗2,j∗2) (i,j=0,1,2...)的点处是数字,两个数字之间是符号,其他位置是‘#’号. 但不知道是理解的问题还是题目描述的问题,数据中还 ...

  2. hdu 5612 Baby Ming and Matrix games

    Baby Ming and Matrix games 题意: 给一个矩形,两个0~9的数字之间隔一个数学运算符(‘+’,’-‘,’*’,’/’),其中’/’表示分数除,再给一个目标的值,问是否存在从一 ...

  3. 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 ...

  4. 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 ...

  5. Baby Ming and Matrix games(dfs计算表达式)

    Baby Ming and Matrix games Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Ja ...

  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. JS高级——沙箱

    基本概念 1.沙箱:与外界隔绝的一个环境,外界无法修改该环境内任何信息,沙箱内的东西单独属于一个世界 2.苹果手的app使用的就是沙箱模式去运行,隔离app的空间,每个app独立运行 js沙箱基本模式 ...

  2. CSS——dispaly、overflow、visibility、opacity

    隐藏盒子: 1.overflow:hidden;             隐藏盒子超出的部分. 2.display: none;                    隐藏盒子,而且不占位置.(用的最 ...

  3. java_randomAccessFile类的使用

    RandomAccessFile java提供的对文件内容的访问,既可以读文件,也可以写文件.RandomAccessFile支持随机访问文件,可以访问文件的任意位置 (1)java文件模型 在硬盘上 ...

  4. mysql5.7初始化密码报错ERROR1820(HY000):YoumustresetyourpasswordusingALTERUSERstateme

    1,mysql5.6是密码为空直接进入数据库的,但是mysql5.7就需要初始密码 cat /var/log/mysqld.log | grep password 或者:grep 'temporary ...

  5. modelsim 仿真软件 百度云分享 modelsim se 10.7 10.6d 10.6c 10.5 10.4

    modelsim se 10.7 链接:https://pan.baidu.com/s/1NDC2yMCZmA4bIRSk2dUiTg 提取码:4l1d 复制这段内容后打开百度网盘手机App,操作更方 ...

  6. MIUI 的参与感

    最近这段时间在看小米联合创始人黎万强写的<参与感>这本书,看完我还挺有感触的.小米相信大家都一定有所耳闻. 2010 年 4 月 6 日            小米公司正式创立. 8 月 ...

  7. 【Android】登陆界面设计

    界面布局 布局其实很简单,用相对布局累起来就可以了,然后注册和记住密码这两个控件放在一个水平线性布局里 界面底部还设置了一个QQ一键登录的入口,可以直接用. 控件的ID命名有点乱 <?xml v ...

  8. pyhon中的内存优化机制

    一.变量的内存地址 python中变量的内存地址可以用id()来查看 >>> a = " >>> id(a) 2502558915696 二.pyhon中 ...

  9. 【codeforces 801D】Volatile Kite

    [题目链接]:http://codeforces.com/contest/801/problem/D [题意] 给你一个凸多边形的n个点; 然后允许你将每个点移动到距离不超过D的范围内; 要求无论如何 ...

  10. 【codeforces 796D】Police Stations

    [题目链接]:http://codeforces.com/contest/796/problem/D [题意] 在一棵树上,保证每个点在距离d之内都有一个警察局; 让你删掉最多的边,使得剩下的森林仍然 ...