hdu

http://acm.hdu.edu.cn/showproblem.php?pid=1010

这题就是问能不能在t时刻走到门口,不能用bfs的原因大概是可能不一定是最短路路径吧。

但是这题要过除了细心外,还需要强力的剪枝。

奇偶性剪枝:参考 http://www.cppblog.com/Geek/archive/2010/04/26/113615.html

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 #include <cmath>
 5 using namespace std;
 6 char map[9][9];
 7 int n,m,t,di,dj;
 8 bool escape;
 9 int dir[4][2]={{0,-1},{0,1},{1,0},{-1,0}};
10 void dfs(int si,int sj,int cnt)
11 {
12     if(cnt>10000) return;
13     if(escape) return;
14    if(si>n||sj>m||si<=0||sj<=0) return;
15    if(cnt==t&&si==di&&sj==dj)    escape=1;
16    if(escape) return;
17    if(cnt>=t) return;
18    int i,temp;
19    temp=(t-cnt)-abs(si-di)-abs(sj-dj);
20    if(temp<0||temp&1) return;
21    for(i=0;i<4;i++){
22       if(map[si+dir[i][0]][sj+dir[i][1]]!='X')
23       {
24          map[si+dir[i][0]][sj+dir[i][1]]='X';
25          dfs(si+dir[i][0],sj+dir[i][1],cnt+1);
26          map[si+dir[i][0]][sj+dir[i][1]]='.';
27       }
28    }
29    return;
30 }
31 int main()
32 {
33     int i,j,si,sj;
34     while(cin>>n>>m>>t)
35     {
36       if(n==0&&m==0&&t==0) break;
37       int wall=0;
38       for(i=1;i<=n;i++)
39          for(j=1;j<=m;j++)
40          {
41             cin>>map[i][j];
42             if(map[i][j]=='S') { si=i; sj=j; }
43             else if(map[i][j]=='D') { di=i; dj=j; }
44             else if(map[i][j]=='X') wall++;
45          }
46        if(n*m-wall<=t)
47        {
48            cout<<"NO"<<endl;
49            continue;
50        }
51        escape=0;
52        map[si][sj]='X';
53        dfs(si,sj,0);
54        if(escape) cout<<"YES"<<endl;
55        else cout<<"NO"<<endl;
56    }
57    return 0;
58 }

http://acm.hdu.edu.cn/showproblem.php?pid=1015

给定一个字符串和一个数n,然后再字符串中找出5个字符,满足题目中的等式并且字典序最大。

输入之后先把字符串从大到小排序,然后搜索即可。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<cmath>
 4 #include<algorithm>
 5 using namespace std;
 6 
 7 bool cmp(char a,char b)
 8 {
 9     return a>b;
10 }
11 
12 int k,j,flag,vis[15];
13 char s[15],ss[6],res[6];
14 
15 bool judge(int v,int w,int x,int y,int z)
16 {
17     if(v-w*w+x*x*x-y*y*y*y+z*z*z*z*z==k)
18         return 1;
19     return 0;
20 }
21 
22 void dfs(int x)
23 {
24     if(flag) return;
25     int i;
26     if(x==5)
27     {
28         if(judge(ss[0]-64,ss[1]-64,ss[2]-64,ss[3]-64,ss[4]-64)) {flag=1;strcpy(res,ss);}
29         return;
30     }
31     int l=strlen(s);
32     for(i=0;i<l;i++)
33     {
34         if(!vis[i])
35         {
36             vis[i]=1;
37             ss[x]=s[i];
38             dfs(x+1);
39             vis[i]=0;
40         }
41     }
42 }
43 
44 int main()
45 {
46     int i;
47     //freopen("a.txt","r",stdin);
48     while(scanf("%d %s",&k,s)!=EOF&&k!=0&&strcmp(s,"END")!=0)
49     {
50         flag=0;
51         sort(s,s+strlen(s),cmp);
52         memset(vis,0,sizeof(vis));
53         dfs(0);
54         if(flag)
55         {
56             printf("%s
",res);
57         }
58         else printf("no solution
");
59     }
60     return 0;
61 }
原文地址:https://www.cnblogs.com/nowandforever/p/4523223.html