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, ...
随机推荐
- nmap 使用脚本引擎进行扫描
1.下载nmap(nmap官网). 2.安装nmap. 3.编辑环境变量(windows下所需),保存.
- 小米路由器mini建FTP
输入命令 df -h 复制代码 查看自己的分区路径,我自己用了一个闲置笔记本电脑硬盘转USB,插到mini,有四个区 为了方便,我选择把ftp安装在第一个分区 /extdisks/sda1/ 创建f ...
- samba错误
1.session setup failed: NT_STATUS_LOGON_FAILURE 该错误表示用户有误, 可能是用户不存在, 也有可能是密码错误, 或者用户只是在samba和系统的用户中的 ...
- GDI+(Graphics Device Interface)
1创建画布(创建Graphics对象) Bitmap bitmap = new Bitmap(80,80); Graphics g=Graphics.FromImage(bitmap); 2创建Pen ...
- 提高jQuery执行效率
1. 使用最新版本的jQuery jQuery的版本更新很快,你应该总是使用最新的版本.因为新版本会改进性能,还有很多新功能. 下面就来看看,不同版本的jQuery性能差异有多大.这里是三条最常见的j ...
- Android与JS混编(js调用android相机)
参考android相机调用,http://blog.csdn.net/yanzi1225627/article/details/33028041/,谢谢 相机怎么调用就不做赘述了,下面是js调用 ...
- 华为Oj 找出字符串第一个出现一次的字符
#include <stdio.h> #include <string.h> char firstSingle(char *str) { int hash[255]={0}; ...
- iOS_SN_Xcode内存泄露调试
用Xcode进行内存调试有两种方法: 1.静态方法 2.动态方法 静态方法是直接在Xcode的菜单栏中选择product-->analyze 如截图所示. 之后会看到Xcode的编译状态上会有如 ...
- OD学习笔记10:一个VB程序的加密和解密思路
前边,我们的例子中既有VC++开发的程序,也有Delphi开发的程序,今天我们给大家分析一个VB程序的加密和解密思路. Virtual BASIC是由早期DOS时代的BASIC语言发展而来的可视化编程 ...
- 洛谷 P1896 互不侵犯King
P1896 [SCOI2005]互不侵犯King 题目描述 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的各一个格子,共 ...