Fork me on GitHub

HDU ACM Deposit Code

复制代码
 1 #include<stdio.h>
 2 #include<string.h>
 3 int prime[2600];
 4 void rotprime()
 5 {
 6     int i, j;
 7     memset(prime, 0, sizeof(prime));
 8     prime[0]=prime[1]=1;
 9     for(i=2; i<2600; ++i)
10     {
11         if(!prime[i])
12         {
13             for(j=2; j*i<2600; j++)
14             prime[j*i] = 1;
15         } 
16     }
17     return;
18 } 
19 
20 int main()
21 {
22     int x, y, flag;
23     rotprime();
24     while(scanf("%d%d", &x, &y) != EOF)
25     {
26         if(!x && !y) break;
27         if(x>y) x^=y,y^=x,x^=y;
28         for(flag=0; x<=y; ++x)
29         if(prime[x*x+x+41]) {
30             flag = 1; printf("Sorry\n");
31             break;
32         }
33         if(!flag) printf("OK\n");
34     }
35     return 0;
36 }
HDU 2012 Code[1] time: 2012-11-23 17:21:06
复制代码
复制代码
 1 #include<stdio.h>
 2 #include<string.h>
 3 int main()
 4 {
 5     int sort[1010], T, n, i, j;
 6     scanf("%d", &T);
 7     while(T--)
 8     {
 9         memset(sort, 0, sizeof(sort));
10         scanf("%d", &n);
11         for(i=1; i<=n; ++i)
12         scanf("%d", &sort[i]);
13         for(i=2; i<=n; ++i)
14         {
15             if(sort[i] < sort[i-1])
16             {
17                 sort[0] = sort[i];
18                 sort[i] = sort[i-1];
19                 for(j=i-2; j>=0; --j)
20                 {
21                     if(sort[0] < sort[j]) sort[j+1] = sort[j];
22                     else {sort[j+1] = sort[0]; break;}
23                 }
24             }
25         }
26         for(i=1; i<=n; ++i)
27         if(i == 1)printf("%d", sort[i]);
28         else printf(" %d", sort[i]);
29         printf("\n");
30         
31     }
32     return 0;
33 }
HDU 1040 Code[1]
复制代码
复制代码
 1 #include<stdio.h>
 2 
 3 int main()
 4 {
 5     int i, count;
 6     double n, res;
 7     while(scanf("%lf", &n) != EOF && n != 0.00)
 8     {
 9         res = 0;
10         count = 1;
11         while(res < n)
12         {
13             count++;
14             res = 1.0/count + res;
15         }
16         printf("%d card(s)\n", count-1);
17     }
18     
19     return 0;
20 }
HDU 1056 Code[1] time:2012-11-23 13:08:43
复制代码
复制代码
 1 /*Author: Joseph
 2  *Problem: Rescue
 3  *State: AC
 4  */
 5 
 6 #include<cstdio>
 7 #include<cstring>
 8 #include<queue>
 9 #define SIZE 202
10 #define ROAD 0
11 #define WALL 1
12 #define ANGEL 2
13 #define GUARD -1
14 
15 using namespace std;
16 
17 int prison[SIZE][SIZE];
18 int step[SIZE][SIZE];
19 int n, m;
20 
21 
22 int dir[][2] = {{0, -1}, {-1, 0}, {0, 1}, {1, 0}};
23 
24 
25 void Traverse(int x, int y, int dx, int dy, int cur)
26 {
27     for(int t=0; t<4; ++t)
28     {
29         int newx = x+dir[t][0];
30         int newy = y+dir[t][1];
31         if(newx >= 0 && newx < n && newy >= 0 && newy < m && prison[newx][newy] != WALL)
32         {
33             int temp = cur+1;
34             if(prison[newx][newy] == GUARD) temp++;
35             if(step[newx][newy] == -1)
36             {
37                 step[newx][newy] = temp;
38                 Traverse(newx, newy, dx, dy, temp);
39             }
40             else if(step[newx][newy] > temp)
41             {
42                 step[newx][newy] = temp;
43                 Traverse(newx, newy, dx, dy, temp);
44             }
45         }
46     }
47     return;
48 }
49 
50 int main()
51 {
52     #ifndef ONLINE_JUDGE
53     freopen("input.txt", "r", stdin);
54     #endif
55     int  sx, sy, dx, dy;
56     char input[SIZE];
57     while(scanf("%d%d", &n, &m) != EOF)
58     {
59         for(int i=0; i<n; ++i)
60         {
61             scanf("%s", input);
62             for(int j=0; j<m; ++j)
63             {        
64                 if(input[j] == '#') prison[i][j] = WALL;
65                 else if(input[j] == 'x') prison[i][j] = GUARD;
66                 else if(input[j] == '.') prison[i][j] = ROAD;
67                 else if(input[j] == 'r')
68                 {
69                     prison[i][j] = WALL;
70                     sx = i;
71                     sy = j;
72                 }
73                 else
74                 {
75                     prison[i][j] = ROAD;
76                     dx = i;
77                     dy = j;
78                 }
79             }    
80         }
81         memset(step, -1, sizeof(step));
82         Traverse(sx, sy, dx, dy, 0);
83         if(step[dx][dy] == -1) printf("Poor ANGEL has to stay in the prison all his life.\n");
84         else printf("%d\n", step[dx][dy]);
85     }
86     return 0;
87 }
HDU 1242 time:2013-7-16 16:38:59
复制代码
复制代码
  1 #include<stdio.h>
  2 #include<malloc.h>
  3 #define MAXN 12
  4 typedef struct Queue{
  5     int value;
  6     int x, y, z;
  7     struct Queue *next; 
  8 }Queue;
  9 
 10 int direction[6][3] = {{-1, 0, 0}, {0, 0, -1}, {0, 1, 0}, {0, 0, 1}, {0, -1, 0}, {1, 0, 0}};
 11 int is_value(int i, int j, int z, int n)
 12 {
 13     return (i>=0 && i<n && j>=0 && j<n && z>=0 && z<n);
 14 }
 15 /*
 16 void Print(int (*maze)[MAXN][MAXN], int l, int r, int c)
 17 {
 18             for(int i=0; i<l; ++i)
 19             {
 20                 for(int j=0; j<r; ++j)
 21                 {
 22                     for(int t=0; t<c; ++t)
 23                     printf("%2d ", maze[i][j][t]);
 24                     printf("\n");    
 25                 }
 26                 printf("\n");
 27             }
 28     return;    
 29 }
 30 */
 31 
 32 void Traverse(int (*maze)[MAXN][MAXN], int x, int y, int z, int dx, int dy, int dz, int n, int step)
 33 {
 34     int k, i, j, t;
 35     Queue *str = NULL, *stp = NULL, *stf = NULL, *tail = NULL;
 36     str = (Queue*)malloc(sizeof(Queue));
 37     str->value = 0, str->x = x, str->y = y, str->z = z, str->next = NULL;
 38     tail = str;
 39     while(str != NULL)
 40     {
 41         
 42         for(k=0; k<6; ++k)
 43         {
 44             i = str->x + direction[k][0];
 45             j = str->y + direction[k][1];
 46             t = str->z + direction[k][2];
 47             if(is_value(i, j ,t, n) && maze[i][j][t] == 0)
 48             {
 49             /*    if(maze[i][j][t] == -2 || (maze[i][j][t] > str->value)) */
 50                 {
 51                     stf = (Queue*)malloc(sizeof(Queue));
 52                     maze[i][j][t] = stf->value = str->value+1;
 53                     stf->x = i, stf->y = j, stf->z = t;
 54                     stf->next = NULL;
 55                     tail->next = stf;
 56                     tail = stf;
 57                     if(i == dx && j == dy && t == dz) return;
 58                 }
 59             } 
 60         }
 61         stp = str->next;
 62         free(str);
 63         str = stp;
 64     }
 65     return;
 66 }
 67 
 68 int main()
 69 {
 70     #ifndef ONLINE_JUDGE
 71     freopen("input.txt", "r", stdin);
 72     #endif
 73     int l, r, c, sx, sy, sz, dx, dy, dz, i, j, t, step, n;
 74     int maze[MAXN][MAXN][MAXN];
 75     char temp, start[MAXN];
 76     
 77     while(scanf("%s", start) != EOF)
 78     {
 79    //     printf("%s\n", start);
 80         scanf("%d", &n);
 81         getchar();
 82         for(i=0; i<n;  ++i)
 83             for(j=0; j<n; ++j)
 84             {
 85                 scanf("%s", start);
 86                 for(t=0; t<n; ++t)
 87                 {
 88                     if(start[t] == 'O') maze[i][j][t] = 0;
 89                     else maze[i][j][t] = -1;
 90           //          printf("%c", start[t]);
 91                 }
 92           //      printf("\n");
 93                     
 94             }
 95          scanf("%d%d%d", &sz, &sy, &sx);
 96          scanf("%d%d%d", &dz, &dy, &dx);
 97     //      printf("%d\n", maze[dx][dy][dz]);
 98          scanf("%s", start);
 99    //      printf("%s\n", start);
100         step = 0;
101         if(sx != dx || sy != dy || sz != dz)
102         {
103             maze[sx][sy][sz] = -1;
104             Traverse(maze, sx, sy, sz, dx, dy, dz, n, step);
105             if(maze[dx][dy][dz] != 0)
106             printf("%d %d\n", n, maze[dx][dy][dz]);
107             else printf("NO ROUTE\n");            
108         }
109         else printf("%d %d\n", n, maze[dx][dy][dz]);
110 
111     }
112     return 0;
113 }
HDU 1240 time: 2013-7-16 16:41:51
复制代码
复制代码
 1 /*Author: xueying
 2  *Problem: Safecracker
 3  *State: 1y
 4  *Type: Search
 5  */
 6 
 7 #include<cstdio>
 8 #include<cstring>
 9 #include<algorithm>
10 
11 bool cmp(const int& a, const int& b)
12 {
13     return a>b;
14 }
15 
16 using namespace std;
17 
18 bool visit[14];
19 int digit[14];
20 char input[14];
21 int aim[5];
22 int target;
23 
24 int fac(int n, int m)
25 {
26     int sum = 1;
27     while(m--) sum *= n;
28     return sum;
29 }
30 
31 bool Traverse(int sum, int cur, int n)
32 {
33     if(cur == 5)
34     {
35         if(sum == target) return true;
36         else return false;
37     }
38     int temp = cur+1, flag;
39     flag = temp%2 ? 1 : -1;
40     for(int i=0; i<n; ++i)
41     {
42         if(!visit[i])
43         {
44             visit[i] = true;
45             aim[cur] = digit[i];
46             if(Traverse(sum+flag*fac(digit[i], temp), temp, n)) return true;
47             visit[i] = false;
48         }
49     }
50     return false;
51 }
52 
53 int main()
54 {
55     #ifndef ONLINE_JUDGE
56     freopen("input.txt", "r", stdin);
57     #endif
58     while(scanf("%d", &target) != EOF)
59     {
60         scanf("%s", input);
61         if(strcmp(input, "END") == 0) break;
62         int len = strlen(input);
63         for(int i=0; i<len; ++i)
64             digit[i] = input[i] - 'A' + 1;
65         sort(digit, digit+len, cmp);
66         bool flag = false;
67         memset(visit, false, sizeof(visit));
68         for(int i=0; i<len; ++i)
69         {
70             aim[0] = digit[i];
71             visit[i] = true;
72             if(Traverse(aim[0], 1, len))
73             {
74                 flag = true;
75                 break;
76             }
77             visit[i] = false;
78         }
79         if(flag)
80         {
81             for(int i= 0; i<5; ++i)
82                 printf("%c", (aim[i]+'A'-1));
83             printf("\n");
84         }
85         else printf("no solution\n");
86     }
87     return 0;
88 }
HDU 1015 time: 2013-7-16 18:12:26
复制代码
复制代码
 1 /*Author: xueying
 2  *Problem: Prime Ring Problem
 3  *State: 1y
 4  *Type: Brute force
 5  */
 6 
 7 #include<cstdio>
 8 #include<cstring>
 9 #include<algorithm>
10 #define SIZE 22
11 
12 using namespace std;
13 
14 int prime[SIZE*2];
15 int visit[SIZE];
16 int digit[SIZE];
17 
18 void get_prime()
19 {
20     memset(prime, -1, sizeof(prime));
21     for(int i=2; i<SIZE*2; ++i)
22         if(prime[i] == -1)
23             for(int j=i+i; j<SIZE*2; j+=i) prime[j] = 0;
24     return;
25 }
26 
27 void Traverse(int cur, int cnt, int n)
28 {
29     if(cnt == n && prime[cur+1])
30     {
31         for(int i=0; i<n; ++i)
32             if(!i) printf("%d", digit[i]);
33             else printf(" %d", digit[i]);
34         printf("\n");
35     }
36     else if(cnt == n) return;
37     
38     for(int i=2; i<=n; ++i)
39     {
40         if(!visit[i] && prime[cur+i])
41         {
42             digit[cnt] = i;
43             visit[i] = 1;
44             Traverse(i, cnt+1, n);
45             visit[i] = 0;
46         }
47     }
48     return;
49 }
50 
51 int main()
52 {
53     #ifndef ONLINE_JUDGE
54 //    freopen("input.txt", "r", stdin);
55     #endif
56     int n, t = 0;
57     get_prime();
58     while(scanf("%d", &n) != EOF)
59     {
60         printf("Case %d:\n", ++t);
61         if(n == 1)
62         {
63             printf("1\n\n");
64             continue;
65         }
66         memset(visit, 0, sizeof(visit));
67         visit[0] = digit[0] = 1;
68         for(int i=2; i<=n; ++i)
69         {
70             if(prime[i+1])
71             {
72                 visit[i]=1;
73                 digit[1] = i;
74                 Traverse(i, 2, n);
75                 visit[i]=0;    
76             }
77         }
78         printf("\n");
79     }
80     return 0;
81 }
HDU 1016 time: 2013-7-16 19:03:08
复制代码
复制代码
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<cmath>
 4 #include<queue>
 5 #include<algorithm>
 6 #include<stdlib.h>
 7 #define SIZE 9
 8 #define INF -1024
 9 #define WALL 0
10 #define NOTHING 1
11 #define START 2
12 #define EXIT 3
13 #define RESET 4
14 #define INIT 6
15 
16 using namespace std;
17 
18 typedef struct condition{
19     int row, column;
20     int time, step;
21 }area;
22 
23 queue<area>run;
24 
25 int laby[SIZE][SIZE];
26 int visit[SIZE][SIZE];
27 int littletime[SIZE][SIZE];
28 int dir[][2] = {{-1, 0}, {1,0}, {0,1}, {0, -1}};
29 int n, m;
30 
31 void Traverse(int x, int y, int dx, int dy)
32 {
33     area begin;
34     begin.row = x, begin.column = y, begin.time = INIT, begin.step = 0;
35     visit[x][y] = 0;
36     if(x == dx && y == dy) return;
37     run.push(begin);
38     bool flag = false;
39     while(!run.empty())
40     {
41         area state;
42         state = run.front();
43         run.pop();
44         int& sx = state.row;
45         int& sy = state.column;
46         for(int i=0; i<4; ++i)
47         {
48             int newx = sx + dir[i][0];
49             int newy = sy + dir[i][1];
50             if(newx>=0 && newx<n && newy>=0 && newy<m && laby[newx][newy] != WALL)
51             {
52                 area next;
53                 next.row = newx, next.column = newy;
54                 if(visit[newx][newy] == -1 || (visit[newx][newy] > state.step+1 || littletime[newx][newy] < state.time-1))
55                 {
56                     next.step = visit[newx][newy] = state.step+1;
57                     littletime[newx][newy] = next.time = state.time-1;
58                     if(newx == dx && newy == dy)
59                     {
60                         flag = true;
61                         break;
62                     }
63                     if(laby[newx][newy] == RESET) next.time = INIT;
64                     if(next.time >= 2) run.push(next);
65                 }
66             }
67         }
68         if(flag) break;
69     }
70     while(!run.empty()) run.pop();
71     return;
72 }
73 
74 
75 int main()
76 {
77     #ifndef ONLINE_JUDGE
78     freopen("input.txt", "r", stdin);
79     #endif
80     int T, sx, sy, dx, dy;
81     scanf("%d", &T);
82     while(T--)
83     {
84         memset(visit, -1, sizeof(visit));
85         scanf("%d%d", &n, &m);
86         for(int i=0; i<n; ++i)
87         for(int j=0; j<m; ++j)
88         {
89             scanf("%d", &laby[i][j]);
90             if(laby[i][j] == START)
91                 sx = i, sy = j;    
92             else if(laby[i][j] == EXIT)
93                 dx = i, dy = j;
94         }
95         Traverse(sx, sy, dx, dy);
96         printf("%d\n", visit[dx][dy]);
97     }
98     return 0;
99 }
HDU 1072 time: 2013-7-17 12:39:15
复制代码
复制代码
 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<malloc.h>
 4 #define MAXN 52
 5 typedef struct Queue{
 6     int value;
 7     int x, y, z;
 8     struct Queue *next;
 9 }Queue;
10 
11 int visit[MAXN][MAXN][MAXN];
12 int direction[6][3] = {{-1, 0, 0}, {0, 0, -1}, {0, 1, 0}, {0, 0, 1}, {0, -1, 0}, {1, 0, 0}};
13 int is_value(int i, int j, int z, int l, int r, int c)
14 {
15     return (i>=0 && i<l && j>=0 && j<r && z>=0 && z<c);
16 }
17 
18 void Traverse(int (*maze)[MAXN][MAXN], int l, int r, int c, int T)
19 {
20     int k, i, j, t, flag;
21     Queue *str = NULL, *stp = NULL, *stf = NULL, *tail = NULL;
22     str = (Queue*)malloc(sizeof(Queue));
23     str->value = 0, str->x = 0, str->y = 0, str->z = 0, str->next = NULL;
24     tail = str;
25     if(l+r+c == 0) {visit[0][0][0] = 0; return;}
26     while(str != NULL)
27     {
28         for(k=0; k<6; ++k)
29         {
30             i = str->x + direction[k][0];
31             j = str->y + direction[k][1];
32             t = str->z + direction[k][2];
33             if(is_value(i, j ,t, l, r, c) && maze[i][j][t] == 0)
34             {
35                 {
36                     stf = (Queue*)malloc(sizeof(Queue));
37                     visit[i][j][t] = stf->value = str->value+1;
38                     stf->x = i, stf->y = j, stf->z = t;
39                     stf->next = NULL;
40                     tail->next = stf;
41                     tail = stf;
42                     if(visit[i][j][t] > T) return;
43                     if(i == l-1 && j == r-1 && t == c-1) return;
44                 }
45             }
46         }
47         stp = str->next;
48         free(str);
49         str = stp;
50     }
51 
52     return;
53 }
54 
55 int main()
56 {
57     #ifndef ONLINE_JUDGE
58     freopen("input.txt", "r", stdin);
59     #endif 
60     int k, T, l, r, c, sx, sy, sz, dx, dy, dz, i, j, t, step;
61     int maze[MAXN][MAXN][MAXN];
62     scanf("%d", &k);
63     while(k--)
64     {
65         scanf("%d%d%d%d", &l, &r, &c, &T);
66         for(i=0; i<l; ++i)
67             for(j=0; j<r; ++j)
68                 for(t=0; t<c; ++t)
69                     scanf("%d", &maze[i][j][t]);
70         memset(visit, -1, sizeof(visit));
71         Traverse(maze, l, r, c, T);
72         printf("%d\n", visit[l-1][r-1][c-1]);
73     }
74     return 0;
75 }
HDU 1253 time: 2013-7-17 14:02:03
复制代码

 

 

 

 

==============

 

 

posted @   Gifur  阅读(233)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
TOP
点击右上角即可分享
微信分享提示