hdoj--5612--Baby Ming and Matrix games(dfs)
2016-03-07 20:34
405 查看
Total Submission(s): 1150 Accepted Submission(s): 298
[align=left]Problem Description[/align]
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.)
[align=left]Input[/align]
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
[align=left]Output[/align]
Print Possible if it is possible to find such an expressions.
Print Impossible if it is impossible to find such an expressions.
[align=left]Sample Input[/align]
3
3 3 24
1*1
+#*
2*8
1 1 1
1
3 3 3
1*0
/#*
2*6
[align=left]Sample Output[/align]
Possible
Possible
Possible
Hint
The first sample:1+2*8=24
The third sample:1/2*6=3
[align=left]Source[/align]
BestCoder Round #69 (div.2)
[align=left]Recommend[/align]
hujie | We have carefully selected several similar problems for you: 5639 5638 5637 5636 5635
传递的参数都是整数,如果遇到比较大的分母可能表达式的值为0,所以传递的b表示分母,遇到除法就把数乘到分母上,加法或者减法就乘以分母然后加减到分子上,最后判断的时候看是否等于sum*b
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
[align=left]Problem Description[/align]
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.)
[align=left]Input[/align]
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
[align=left]Output[/align]
Print Possible if it is possible to find such an expressions.
Print Impossible if it is impossible to find such an expressions.
[align=left]Sample Input[/align]
3
3 3 24
1*1
+#*
2*8
1 1 1
1
3 3 3
1*0
/#*
2*6
[align=left]Sample Output[/align]
Possible
Possible
Possible
Hint
The first sample:1+2*8=24
The third sample:1/2*6=3
[align=left]Source[/align]
BestCoder Round #69 (div.2)
[align=left]Recommend[/align]
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; }
相关文章推荐
- 2.C语言数据类型
- NYOJ 92 图像有用区域 (经典的 bfs)
- JS返回顶部的几种方法
- hdoj--5612--Baby Ming and Matrix games(dfs)
- halo world ))) uva 10934 droping water
- MySQL批量SQL插入性能优化
- JS常用事件
- 操作系统相关资料之三
- 平时的小问题(一直更新渴求交流)
- Java中final、finally、finalize的区别
- PHP数据加密技术之一---MD5()和Sha1()加密
- bzoj3687: 简单题
- Item 24: 区分右值引用和universal引用
- java后台訪问url连接——HttpClients
- Java设计模式学习笔记(单例模式)
- android 学习笔记之20160307自定义对话框
- dfs(顺便求一下每个节点的儿子节点喽)
- 目标检测的图像特征提取之(二)LBP特征
- Open quote is expected for attribute "class" associated with an elemen的解决方案
- Spring Security权限管理框架