2013/7/30 JNU周练

A - HDU1372 Knight Moves

搜索入门题

 1 /*
 2     搜索经典入门题
 3     一个8*8的棋盘,给定起点坐标和终点坐标,求马(马走日)由起点到终点需要走多少步
 4     广搜一遍即可
 5 */
 6 #include <cstdio>
 7 #include <cstring>
 8 #include <iostream>
 9 #include <queue>
10 using namespace std;
11 
12 #define N 10
13 int ans[N][N],vis[N][N];
14 //用于模拟马一步可能去的八个位置
15 int dir[9][2]={{-2,1},{-2,-1},{2,1},{2,-1},{-1,2},{-1,-2},{1,2},{1,-2}};
16 struct Node
17 {
18     int x,y;
19 };
20 char a[5],b[5];
21 queue<Node> q;
22 
23 //x1,y1为起点坐标,x2,y2为终点坐标
24 void bfs(int x1, int y1, int x2, int y2)
25 {
26     while(!q.empty()) q.pop();
27     //memset(ans,0,sizeof(ans));
28     memset(vis,0,sizeof(vis));
29     ans[x1][y1] = 0;
30     Node tmp;
31     tmp.x = x1; tmp.y = y1;
32     q.push(tmp);
33     vis[x1][y1] = 1;
34 
35     //q.empty()表明棋盘上能走到的点都走遍
36     while(!q.empty())
37     {
38         tmp = q.front();
39         q.pop();
40         //找到到达终点步数即可退出
41         if(tmp.x == x2 && tmp.y == y2)
42             return;
43         //往八个方向走,若抵达的点尚未走过,则入队
44         for(int i=0; i<8; i++)
45         {
46             int tx = tmp.x + dir[i][0];
47             int ty = tmp.y + dir[i][1];
48             if(tx>=1 && tx<=8 && ty >= 1 && ty <=8 && vis[tx][ty]==0)
49             {
50                 ans[tx][ty] = ans[tmp.x][tmp.y] + 1;
51                 vis[tx][ty] = 1;
52                 Node tmp2;
53                 tmp2.x = tx; tmp2.y = ty;
54                 q.push(tmp2);
55             }
56         }
57     }
58 }
59 
60 int main()
61 {
62     while(scanf("%s %s",a,b)!=EOF)
63     {
64         int x1 = a[0] - 'a' + 1;
65         int y1 = a[1] - '0';
66         int x2 = b[0] - 'a' + 1;
67         int y2 = b[1] - '0';
68         bfs(x1,y1,x2,y2);
69         printf("To get from %s to %s takes %d knight moves. ",a,b,ans[x2][y2]);
70     }
71     return 0;
72 }
View Code

B - POJ2503 Babelfish

字典查询

普通版(map | qsort+bsearch)

  1 /*
  2     map string 版  985ms
  3     知识点补充:
  4     1、gets():可接受回车键之前输入的所有字符,并用' '替代 ''.回车键不会留在输入缓冲区中
  5        scanf() :当遇到回车,空格和tab键会自动在字符串后面添加'',但是回车,空格和tab键仍会留在输入的缓冲区中。
  6        gets(string); ---> 遇到回车认为输入结束
  7        scanf("%s",string); ---> 遇到空格认为输入结束
  8     2、m[b] = a;自动转换成string
  9     3、sscanf与scanf类似,都是用于输入的,只是后者以屏幕(stdin)为输入源,前者以固定字符串为输入源。
 10 
 11 #include <iostream>
 12 #include <cstdio>
 13 #include <map>
 14 #include <string>
 15 #include <cstring>
 16 using namespace std;
 17 
 18 int main()
 19 {
 20     char a[11],b[11];
 21     char str[30];
 22     map<string,string> m;
 23     while(gets(str))
 24     {
 25         if(strlen(str)==0) break;
 26         sscanf(str,"%s%s",a,b);
 27         m[b] = a;
 28     }
 29 
 30     while(gets(str))
 31     {
 32         sscanf(str,"%s",a);
 33         if(m[a].length()==0)
 34             cout << "eh" << endl;
 35         else
 36             cout << m[a] << endl;
 37     }
 38     return 0;
 39 }
 40 */
 41 /*
 42     快排+二分查找
 43     手写二分果断超时,用C标准库的bsearch(),375ms
 44     知识点补充:
 45     bsearch()函数:
 46     void *bsearch(const void *key, const void *base, size_t nitems, size_t size, int (*compar)(const void *, const void *))
 47     第一个参数为要查找的值,后面的参数与快排函数的参数类似,其中比较函数返回0表示匹配成功
 48 
 49 
 50 #include <stdio.h>
 51 #include <string.h>
 52 #include <stdlib.h>
 53 #define N 100005
 54 
 55 struct Dic
 56 {
 57     char pre[11],to[11];
 58 }dic[N];
 59 
 60 //qsort函数
 61 int cmp(const void *a, const void *b)
 62 {
 63     return strcmp(((struct Dic*)a)->pre, ((struct Dic*)b)->pre);
 64 }
 65 
 66 //bsearch函数 (==0)
 67 int bcmp(const void *a, const void *b)
 68 {
 69     return strcmp((char*)a, ((struct Dic*)b)->pre);
 70 }
 71 
 72 //手写二分(超时)
 73 int bisearch(char a[], int n)
 74 {
 75     int l = 0; int r = n - 1;
 76     while(l!=r)
 77     {
 78         int mid = (l + r) / 2;
 79         int flag = strcmp(a,dic[mid].pre);
 80         if(flag == 0)
 81             return mid;
 82         else if(flag < 0)
 83             r = mid - 1;
 84         else
 85             l = mid + 1;
 86     }
 87     return -1;
 88 }
 89 
 90 int main()
 91 {
 92     char str[30];
 93     int n = 0;
 94     while(gets(str))
 95     {
 96         if(strlen(str)==0) break;
 97         sscanf(str,"%s%s",dic[n].to,dic[n].pre);
 98         n++;
 99     }
100     qsort(dic,n,sizeof(Dic),cmp);
101     while(scanf("%s",str)!=EOF)
102     {
103         Dic *p = (Dic*)bsearch(str,dic,n,sizeof(Dic),bcmp);
104         if(p)
105             printf("%s ",p->to);
106         else
107             printf("eh ");
108     }
109     return 0;
110 }
111 */
View Code
原文地址:https://www.cnblogs.com/byluoluo/p/3226719.html