HDU 5612 Baby Ming and Matrix games(dfs暴力)

Baby Ming and Matrix games

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1403    Accepted Submission(s): 382


Problem Description
These few days, Baby Ming is addicted to playing a matrix game.

Given a nm matrix, the character in the matrix (i2,j2) (i,j=0,1,2...) are the numbers between 09 . 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 )

1T1000
 

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 大体题意: 给你个n*n的字符串。 n一定是奇数,其中包括数字(在奇数行或者奇数列) 包括运算符号(偶数行或者偶数列) 还有包括没用的字符# 不应该计算! 思路: dfs 暴力搜索: 遍历字符串,发现一个数字直接dfs。 在dfs中,判断和sum相等 直接return true;(要加精度控制,因为涉及除法,用double ,就得加eps) 然后4个方向走两个单位,因为符号是走一个单位,当走两个单位合法时,那么一个单位走到符号位也是合法的! 然后在dfs传参数即可! 在一个就是除法时,要先判断分母是否为0,是0 则不计算! 教训: 数组大小一定要合理,这个题目数组开小,竟然是WA~~T_T!!
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<cctype>
using namespace std;
int n,m;
double sum;
const int maxn = 105;
const double eps = 1e-8;
bool vis[maxn][maxn];
char s[maxn][maxn];
const int dx[] = {2,0,-2,0};
const int dy[] = {0,-2,0,2};
void init(){
    memset(vis,0,sizeof vis);
}
bool dfs(int x,int y,double ans){
    if (fabs(ans-sum) < eps)return true;
    for (int i = 0; i < 4; ++i){
        int xx = x + dx[i];
        int yy = y + dy[i];
        int x2 = x + dx[i]/2;
        int y2 = y + dy[i]/2;
        if (xx >= 1 && xx <= n && yy >= 1 && yy <= m && !vis[xx][yy] && s[xx][yy] != '#'){
            if (s[x2][y2] == '+'){
                vis[xx][yy] = 1;
                if (dfs(xx,yy,ans + s[xx][yy]-48))return true;
                vis[xx][yy] = 0;
            }
            if (s[x2][y2] == '-'){
                vis[xx][yy] = 1;
                if (dfs(xx,yy,ans - (s[xx][yy]-48)))return true;
                vis[xx][yy] = 0;
            }
            if (s[x2][y2] == '*'){
                vis[xx][yy] = 1;
                if (dfs(xx,yy,ans * (s[xx][yy]-48)))return true;
                vis[xx][yy] = 0;
            }
            if (s[x2][y2] == '/' && s[xx][yy]!=48){
                vis[xx][yy] = 1;
                if (dfs(xx,yy,ans / (s[xx][yy]-48)))return true;
                vis[xx][yy] = 0;
            }
        }
    }
    return false;

}
int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        init();
        scanf("%d%d%lf",&n,&m,&sum);
        for (int i = 1; i <= n; ++i)
            scanf("%s",s[i] + 1);
        bool ok = false;
        for (int i = 1; i <= n; ++i){
            for (int j = 1; j <= m; ++j){
                if (isdigit(s[i][j])){
                    memset(vis,0,sizeof vis);
                    vis[i][j] = 1;
                    if (dfs(i,j,s[i][j]-48)){
                        ok=true;
                        goto TT;
                    }
                }
            }
        }
        TT:
            printf("%s",ok ? "Possible" : "Impossible");
            printf("\n");
    }
    return 0;
}

 

你可能感兴趣的:(C语言)