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 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.)
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(−<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≤
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

*
+#*
* *
/#*
*
Sample Output
Possible
Possible
Possible
Hint
The first sample:+*=
The third sample:/*=
Source
 
    • 题意: 
      给一个矩形,两个0~9的数字之间隔一个数学运算符(‘+’,’-‘,’*’,’/’),其中’/’表示分数除,再给一个目标的值,问是否存在从一个数字出发,以数字之间的运算符为运算,得到这个目标值;(每个数字只能用一次,其实说白了就是dfs..);可以则输出(Impossible),否则输出(Possible);
     直接dfs暴力即可,注意要用double类型来计算
 
AC代码:
 #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 4496 D-City —— (并查集的应用)


下一篇:pc端结合canvas实现简单签名功能