您的位置:首页 > 运维架构

DFS·穷竭搜索· POJ - 3050·Hopscotch

2018-03-15 10:49 369 查看
题目大意:
给你一个5*5矩阵,最多走五次, 问能组成的不重复数字的数量书多少;
解题思路:
DFS枚举。。。
AC代码:#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <bitset>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <fstream>
#include <cstdlib>
#include <sstream>
#include <cstring>
#include <iostream>
#include <algorithm>
#pragma comment(linker, "/STACK:1024000000,1024000000")

using namespace std;
#define maxn 1010
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define ms(x,y) memset(x,y,sizeof(x))
#define rep(i,n) for(int i=0;i<(n);i++)
#define repf(i,a,b) for(int i=(a);i<=(b);i++)
#define pii pair<int,int>
//#define mp make_pair
#define FI first
#define SE second
#define IT iterator
#define PB push_back
#define Times 10
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int ,int > P;

const double eps = 1e-10;
const double pi = acos(-1.0);
const ll mod = 1e9+7;
const int inf = 0x3f3f3f3f;
const ll INF = (ll)1e18+300;
const int maxd = 50000 + 5;

int mapp[10][10];
set<int > s;
int vis[10][10];
int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

bool judge(int x, int y) {
if(x >= 0 && x < 5 && y >= 0 && y < 5) {
return true;
}
return false;
}

void dfs(int x, int y, int ceng, int num) {
if(ceng == 5) {
s.insert(num);
return ;
}
//vis[x][y] = 1;
for (int i = 0; i < 4; i++) {
int dx = x + dir[i][0];
int dy = y + dir[i][1];
if(judge(dx, dy)) {
dfs(dx, dy, ceng + 1, num * 10 + mapp[dx][dy]);
}
}
}

int main() {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
cin >> mapp[i][j];
}
}
for (int i = 0; i < 5; i ++) {
for (int j = 0; j < 5; j++) {
//ms(vis, 0);
dfs(i, j, 0, mapp[i][j]);
}
}
cout << s.size() << endl;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: