生活随笔
收集整理的這篇文章主要介紹了
poj3050【dfs】
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題意:
5*5的矩陣里,某個點能夠上下左右走,走5步,然后路徑會形成一個串,問你,這個5*5里面能夠形成多少個不同個串。
思路:
直接暴搜,然后對于一個串塞到set里去,然后輸出set里的個數就好了
#include<cstdio>
#include<map>
#include<set>
#include<string>
#include<queue>
#include<math.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define eps 1e-8
typedef __int64 LL;
int dx[
4]={
0,
0,
1,-
1};
int dy[
4]={
1,-
1,
0,
0};
string temp;
set<string>ans;
int a[
6][
6];
void dfs(
int x,
int y,
int num)
{
if(num==
6){ans.insert(temp);
return;}
for(
int i=
0;i<
4;i++){
int xx=dx[i]+x;
int yy=dy[i]+y;
if(xx<
0||yy<
0||xx>=
5||yy>=
5)
continue;temp.push_back(a[xx][yy]+
'0');dfs(xx,yy,num+
1);temp.resize(temp.size()-
1);}
}
int main()
{
for(
int i=
0;i<
5;i++)
for(
int j=
0;j<
5;j++)
scanf(
"%d",&a[i][j]);
for(
int i=
0;i<
5;i++)
for(
int j=
0;j<
5;j++){temp=a[i][j]+
'0';dfs(i,j,
1);}
printf(
"%d\n",ans.size());
return 0;
}
轉載于:https://www.cnblogs.com/keyboarder-zsq/p/5934884.html
總結
以上是生活随笔為你收集整理的poj3050【dfs】的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。