hdu 5612 Baby Ming and Matrix games(dfs暴力)
These few days, Baby Ming is addicted to playing a matrix game. Given a n∗m matrix, the character in the matrix(i∗,j∗) (i,j=,,...) are the numbers between −. 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 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(−<sum<, divisor 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 ) ≤T≤
Print Possible if it is possible to find such an expressions. Print Impossible if it is impossible to find such an expressions.
*
+#*
* *
/#*
*
Possible
Possible
Possible
The first sample:+*=
The third sample:/*=
- 题意:
给一个矩形,两个0~9的数字之间隔一个数学运算符(‘+’,’-‘,’*’,’/’),其中’/’表示分数除,再给一个目标的值,问是否存在从一个数字出发,以数字之间的运算符为运算,得到这个目标值;(每个数字只能用一次,其实说白了就是dfs..);可以则输出(Impossible),否则输出(Possible);
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 26
#define inf 1e12
int n,m;
double sum;
char mp[N][N];
int dirx[]={,,-,};
int diry[]={-,,,};
int vis[N][N],flag;
void dfs(int x,int y,double s){
vis[x][y]=;
if(fabs(s-sum)<=0.00000001){
flag=;
return;
}
for(int i=;i<;i++){
int fx = x+dirx[i],fy = y+diry[i];
int sx = x+dirx[i]*,sy = y + diry[i]*;
if(sx< || sx>=n || sy< || sy>=m || vis[sx][sy]) continue;
if(mp[fx][fy]=='#') continue;
double cnt = (double)(mp[sx][sy]-'');
if(mp[fx][fy]=='+') dfs(sx,sy,s+cnt);
else if(mp[fx][fy]=='-') dfs(sx,sy,s-cnt);
else if(mp[fx][fy]=='*') dfs(sx,sy,s*cnt);
else if(mp[fx][fy]=='/') dfs(sx,sy,s/cnt);
}
vis[x][y]=; }
int main()
{
int t;
scanf("%d",&t);
while(t--){
memset(vis,,sizeof(vis));
scanf("%d%d%lf",&n,&m,&sum);
for(int i=;i<n;i++){
scanf("%s",mp[i]);
}
flag=;
for(int i=;i<n;i++){
for(int j=;j<m;j++){
if(mp[i][j]>='' && mp[i][j]<=''){
dfs(i,j,mp[i][j]-'');
if(flag) break;
}
}
if(flag) break;
}
if(flag) printf("Possible\n");
else printf("Impossible\n");
}
return ;
}
别人的AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string.h>
#include<algorithm>
#include<map>
#include<queue>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<time.h>
using namespace std;
#define rep(i,n) for(int i = 1;i <= n;i++)
#define MS0(a) memset(a,0,sizeof(a))
#define esp 1e-6
int n,m,vis[][],f;
double sum;
char s[][];
int dir[][] = {{,,,-},{,,-,}};
double cal(double val,double v,char op)
{
if(op == '+') return val + v;
else if(op == '-') return val - v;
else if(op == '*') return val * v;
return val/v;
}
void dfs(int i,int j,double val)
{
if(fabs(val - sum) < esp) f = ;
for(int k = ;k < && f;k++){
int x = i + *dir[][k] , y = j + *dir[][k];
char op = s[i + dir[][k]][j + dir[][k]];
if(x < || x > n || y < || y > m || vis[x][y]) continue;
int v = s[x][y] - '';
if(op == '/' && v == ) continue;
vis[x][y] = ;
dfs(x,y,cal(val,v,op));
vis[x][y] = ;
}
}
int main()
{
int T,i,j;
cin>>T;
while(T--){
f= ;MS0(vis);
scanf("%d%d%lf",&n,&m,&sum);
rep(i,n) scanf("%s",s[i] + );
for(i = ;i <= n && f;i += )
for(j = ;j <= m && f;j += ){
vis[i][j] = ;
dfs(i,j,s[i][j] - '');
vis[i][j] = ;
}
puts(f?"Impossible":"Possible");
}
return ;
}
hdu 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 ...
- 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 5614 Baby Ming and Matrix tree 树链剖分
题意: 给出一棵树,每个顶点上有个\(2 \times 2\)的矩阵,矩阵有两种操作: 顺时针旋转90°,花费是2 将一种矩阵替换为另一种矩阵,花费是10 树上有一种操作,将一条路经上的所有矩阵都变为 ...
- 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 ...
- hdu 5610 Baby Ming and Weight lifting
Problem Description Baby Ming is fond of weight lifting. He has a barbell pole(the weight of which c ...
- hdu 1010 Tempter of the Bone(dfs暴力)
Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. However, ...
随机推荐
- 获取java类和方法名
String clazz = this.getClass().getName(); String method = Thread.currentThread() .getStackTrace()[1] ...
- VCS仿真生成fsdb文件(Verilog)
VCS仿真生成fsdb文件(Verilog) 一.环境 Linux 平台 csh环境 VCS 64bit Verdi3 二.开始仿真 1. 联合仿真环境配置 a.在testbench中加入如下语句: ...
- 安装mysql时提示The host 'xxx' could not be looked up with resolveip的解决办法
1.首先用cat查看/etc/hosts文件,会显示以下内容: 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.loca ...
- .NET基础拾遗(4)委托和事件1
一.委托初窥:一个拥有方法的对象 (1)本质:持有一个或多个方法的对象:委托和典型的对象不同,执行委托实际上是执行它所“持有”的方法. (2)如何使用委托? ①声明委托类型(delegate关键字) ...
- (转)android客户端从服务器端获取json数据并解析的实现代码
今天总结一下android客户端从服务器端获取json数据的实现代码,需要的朋友可以参考下 首先客户端从服务器端获取json数据 1.利用HttpUrlConnection 复制代码 ...
- 基于nginx的HLS简单服务器搭建
一,首先搭建nginx服务器: 1.1,选定源码目录 选定目录 /usr/local/HLS cd /usr/local/HLS 1.2,安装PCRE库 cd /usr/local/HLS 到www. ...
- poj3258 二分 最小值最大化问题
River Hopscotch Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10842 Accepted: 4654 ...
- C++中的cout输出机制
代码: #include <iostream> using namespace std; int hello(){ cout<<"hello"<< ...
- ZigZag-LeetCode
题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...
- jar打包命令
jar查看jar命令的使用 1. jar cvf 生成jar包的完整名称 要生成的class文件所在目录以及名称 jar -cvf aa.jar *.* 2.将jar包放到环境变量中,可以不用把cla ...