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

Enum:Hopscotch(POJ 3050)

2015-10-10 15:10 288 查看
            


              跳格子

  题目大意:牛像我们一样跳格子,一个5*5的方格,方格有数字,给牛跳5次,可以组成一个6个数字组合字符串,请问能组合多少个字符串?

  题目规模很小,暴力枚举,然后用map这个玩具来检测存不存在就可以了,水题

  

#include <iostream>
#include <functional>
#include <algorithm>
#include <string>
#include <map>

using namespace std;

static int matrix[5][5];//图
static int ans;
string s_tmp = "000000";
map<string, char>dp;

void Search(const int, const int,const int);

int main(void)
{
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++)
scanf("%d", &matrix[i][j]);

for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++)
Search(i, j, 0);
cout << ans << endl;

return 0;
}

void Search(const int y, const int x,const int level)
{
if (level == 6)
{
if (dp.find(s_tmp) == dp.end())
{
dp[s_tmp] = 1;
ans++;
}
}
else
{
s_tmp[level] = matrix[y][x] + '0';
if (y - 1 >= 0)
Search(y - 1, x, level + 1);
if (y + 1 < 5)
Search(y + 1,x, level + 1);
if (x - 1 >= 0)
Search(y, x - 1, level + 1);
if (x + 1 < 5)
Search(y, x + 1, level + 1);
}
}


  


  这速度差不多了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: