superman

聚精会神搞建设 一心一意谋发展
posts - 190, comments - 17, trackbacks - 0, articles - 0
   :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

  1 #include <iostream>
  2 
  3 using namespace std;
  4 
  5 enum { N, E, S, W };
  6 
  7 struct Point
  8 {
  9     int x, y;
 10 
 11     const Point next(int dir)
 12     {
 13         Point nextP;
 14         switch (dir)
 15         {
 16             case N : nextP.x = x - 1, nextP.y = y; break;
 17             case E : nextP.x = x, nextP.y = y + 1break;
 18             case S : nextP.x = x + 1, nextP.y = y; break;
 19             case W : nextP.x = x, nextP.y = y - 1break;
 20         }
 21         return nextP;
 22     }
 23     bool operator == (const Point & p)
 24     {
 25         return x == p.x && y == p.y;
 26     }
 27 }   ;
 28 
 29 char map[10][10];
 30 
 31 bool inside(const Point & p)
 32 {
 33     return p.x >= 0 && p.x < 10 && p.y >= 0 && p.y < 10;
 34 }
 35 bool couldReach(const Point & p)
 36 {
 37     return map[p.x][p.y] == '.';
 38 }
 39 
 40 int main()
 41 {
 42     freopen("ttwo.in""r", stdin);
 43     freopen("ttwo.out""w", stdout);
 44 
 45     Point cowStartP, JohnStartP;
 46 
 47     for (int i = 0; i < 10; i++)
 48     for (int j = 0; j < 10; j++)
 49     {
 50         cin >> map[i][j];
 51 
 52         if (map[i][j] == 'C')
 53             cowStartP.x = i, cowStartP.y = j, map[i][j] = '.';
 54         if (map[i][j] == 'F')
 55             JohnStartP.x = i, JohnStartP.y = j, map[i][j] = '.';
 56     }
 57 
 58     int curTime = 0;
 59     int ReachTime[10][10][4][10][10][4];
 60 
 61     memset(ReachTime, 255sizeof(ReachTime));
 62 
 63     Point cowCurP = cowStartP, JohnCurP = JohnStartP;
 64     int cowCurDir = N, JohnCurDir = N;
 65 
 66     while (true)
 67     {
 68         if (cowCurP == JohnCurP)
 69         {
 70             cout << curTime << endl;
 71             return 0;
 72         }
 73 
 74         if (ReachTime[cowCurP.x][cowCurP.y][cowCurDir][JohnCurP.x][JohnCurP.y][JohnCurDir] != -1)
 75             break;
 76         else
 77             ReachTime[cowCurP.x][cowCurP.y][cowCurDir][JohnCurP.x][JohnCurP.y][JohnCurDir] = curTime;
 78 
 79         Point cowNextP, JohnNextP;
 80 
 81         cowNextP = cowCurP.next(cowCurDir);
 82         if (inside(cowNextP) && couldReach(cowNextP))
 83             cowCurP = cowNextP;
 84         else
 85             cowCurDir = (cowCurDir + 1% 4;
 86 
 87         JohnNextP = JohnCurP.next(JohnCurDir);
 88         if (inside(JohnNextP) && couldReach(JohnNextP))
 89             JohnCurP = JohnNextP;
 90         else
 91             JohnCurDir = (JohnCurDir + 1% 4;
 92 
 93         curTime++;
 94     }
 95 
 96     cout << 0 << endl;
 97 
 98     return 0;
 99 }
100 

posted @ 2009-04-23 10:54 superman 阅读(93) | 评论 (0)编辑 收藏

 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     freopen("concom.in""r", stdin);
 8     freopen("concom.out""w", stdout);
 9 
10     int n;
11     int m[101][101= { 0 };
12 
13     cin >> n;
14     for (int i = 0; i < n; i++)
15     {
16         int a, b, c;
17         cin >> a >> b >> c;
18 
19         m[a][b] = c;
20     }
21 
22     bool control[101][101= { false };
23 
24     for (int p = 1; p <= 100; p++)
25     {
26         int x[101= { 0 };
27         for (int i = 1; i <= 100; i++)
28             x[i] = m[p][i];
29 
30         while (true)
31         {
32             int i;
33             for (i = 1; i <= 100; i++)
34                 if (x[i] > 50 && control[p][i] == false)
35                 {
36                     control[p][i] = true;
37                     for (int j = 1; j <= 100; j++)
38                         x[j] += m[i][j];
39                     break;
40                 }
41             if (i > 100)
42                 break;
43         }
44     }
45 
46     for (int i = 1; i <= 100; i++)
47     for (int j = 1; j <= 100; j++)
48         if (control[i][j] && i != j)
49             cout << i << ' ' << j << endl;
50 
51     return 0;
52 }
53 

posted @ 2009-04-22 16:59 superman 阅读(70) | 评论 (0)编辑 收藏

 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     freopen("money.in""r", stdin);
 8     freopen("money.out""w", stdout);
 9 
10     int v, n;
11     int coin[26];
12 
13     cin >> v >> n;
14     for (int i = 1; i <= v; i++)
15         cin >> coin[i];
16 
17     long long cnt[26][10001= { 0 };
18 
19     for (int i = 1; i <= v; i++)
20         cnt[i][0= 1;
21 
22     for (int i = 1; i <= v; i++)
23         for (int j = 1; j <= n; j++)
24         {
25             cnt[i][j] = cnt[i - 1][j];
26             if (j - coin[i] >= 0)
27                 cnt[i][j] += cnt[i][j - coin[i]];
28         }
29 
30     cout << cnt[v][n] << endl;
31 
32     return 0;
33 }
34 

posted @ 2009-04-20 10:05 superman 阅读(62) | 评论 (0)编辑 收藏

 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 int n;
 6 string s;
 7 
 8 int calc()
 9 {
10     int sum = 0;
11 
12     int p = 0;
13     while (true)
14     {
15         int i = p + 1, cn = 0;
16         while (i < s.size() && (isdigit(s[i]) || s[i] == ' '))
17         {
18             if (isdigit(s[i]))
19                 cn = cn * 10 + (s[i] - '0');
20             i++;
21         }
22 
23         if (s[p] == '+')
24             sum += cn;
25         if (s[p] == '-')
26             sum -= cn;
27 
28         if (i == s.size())
29             break;
30         else
31             p = i;
32     }
33 
34     return sum;
35 }
36 
37 void addOperator(int i, int p)
38 {
39     if (i == n - 1)
40     {
41         if (calc() == 0)
42             cout << s.c_str() + 1 << endl;
43         return;
44     }
45 
46     s[p] = ' ';
47     addOperator(i + 1, p + 2);
48 
49     s[p] = '+';
50     addOperator(i + 1, p + 2);
51 
52     s[p] = '-';
53     addOperator(i + 1, p + 2);
54 }
55 
56 int main()
57 {
58     freopen("zerosum.in""r", stdin);
59     freopen("zerosum.out""w", stdout);
60 
61     cin >> n;
62 
63     s += '+';
64     for (int i = 1; i <= n; i++)
65     {
66         s += (i + '0');
67         if (i < n)
68             s += ' ';
69     }
70 
71     addOperator(02);
72 
73     return 0;
74 }
75 

posted @ 2009-04-19 23:07 superman 阅读(93) | 评论 (0)编辑 收藏

 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     freopen("nocows.in""r", stdin);
 8     freopen("nocows.out""w", stdout);
 9 
10     int n, m;
11     int cnt[100][200= { 0 };
12 
13     cin >> n >> m;
14 
15     for (int i = 1; i <= m; i++)
16         cnt[i][1= 1;
17 
18     for (int i = 2; i <= m; i++)
19         for (int j = 3; j <= n; j += 2)
20             for (int k = 1; k <= j - 2; k += 2)
21                 cnt[i][j] = (cnt[i][j] + cnt[i - 1][k] * cnt[i - 1][j - k - 1]) % 9901;
22 
23     cout << (cnt[m][n] - cnt[m - 1][n] + 9901% 9901 << endl;
24 
25     return 0;
26 }
27 

posted @ 2009-04-19 22:28 superman 阅读(68) | 评论 (0)编辑 收藏

 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     freopen("prefix.in""r", stdin);
 8     freopen("prefix.out""w", stdout);
 9 
10     string word[202];
11     string s;
12     int n;
13 
14     //----read----
15     for (n = 0true; n++)
16     {
17         cin >> word[n];
18         if (word[n] == ".")
19             break;
20     }
21     while(true)
22     {
23         string ts;
24         if (cin >> ts)
25             s += ts;
26         else
27             break;
28     }
29 
30     //----init----
31     sort(word, word + n);
32 
33     //----slove----
34     bool x[200002= { false };
35 
36     for (int i = 0; i < n; i++)
37         if (s.substr(0, word[i].size()) == word[i])
38             x[word[i].size() - 1= true;
39 
40     for (int i = 1; i < s.size(); i++)
41         if (x[i - 1== true)
42             for (int j = 0; j < n; j++)
43             {
44                 if (s[i] > word[j][0])
45                     continue;
46                 if (s[i] < word[j][0])
47                     break;
48 
49                 int k;
50                 for (k = 0; k < word[j].size(); k++)
51                     if (s[i + k] != word[j][k])
52                         break;
53                 if (k == word[j].size())
54                     x[i + word[j].size() - 1= true;
55             }
56 
57     //----output ans----
58     int ans = 0;
59     for (int i = s.size() - 1; i >= 0; i--)
60         if (x[i])
61         {
62             ans = i;
63             break;
64         }
65 
66     if (ans == 0)
67         cout << 0 << endl;
68     else
69         cout << ans + 1 << endl;
70 
71     return 0;
72 }
73 

posted @ 2009-03-31 13:43 superman 阅读(126) | 评论 (0)编辑 收藏

  1 #include <iostream>
  2 
  3 using namespace std;
  4 
  5 const int ON = 1, OFF = -1;
  6 
  7 int n, c;
  8 int FinalStatus[100];
  9 
 10 string ans[16];
 11 int ans_cnt;
 12 
 13 bool x[100];
 14 
 15 string x2string(bool x[], int n)
 16 {
 17     string ts;
 18     for (int i = 0; i < n; i++)
 19         ts += (x[i] + '0');
 20     return ts;
 21 }
 22 
 23 void addAns()
 24 {
 25     string ts = x2string(x, n);
 26 
 27     int i;
 28     for (i = 0; i < ans_cnt; i++)
 29         if (ans[i] == ts)
 30             break;
 31     if (i == ans_cnt)
 32         ans[ans_cnt++= ts;
 33 }
 34 
 35 bool check(int k)
 36 {
 37     if (k == 0)
 38     {
 39         if (c % 2 != 0)
 40             return false;
 41     }
 42     else
 43         if(c % k)
 44             return false;
 45     for (int i = 0; i < n; i++)
 46     {
 47         if (FinalStatus[i] == OFF && x[i] == true)
 48             return false;
 49         if (FinalStatus[i] == ON && x[i] == false)
 50             return false;
 51     }
 52     return true;
 53 }
 54 
 55 void Button_4(int k)
 56 {
 57     if (check(k))
 58         addAns();
 59 
 60     for (int i = 0; i < n; i += 3) x[i] ^= 1;
 61     if (check(k + 1))
 62         addAns();
 63     for (int i = 0; i < n; i += 3) x[i] ^= 1;
 64 }
 65 
 66 void Button_3(int k)
 67 {
 68     Button_4(k);
 69 
 70     for (int i = 0; i < n; i += 2) x[i] ^= 1;
 71     Button_4(k + 1);
 72     for (int i = 0; i < n; i += 2) x[i] ^= 1;
 73 }
 74 
 75 void Button_2(int k)
 76 {
 77     Button_3(k);
 78 
 79     for (int i = 1; i < n; i += 2) x[i] ^= 1;
 80     Button_3(k + 1);
 81     for (int i = 1; i < n; i += 2) x[i] ^= 1;
 82 }
 83 
 84 void Button_1(int k)
 85 {
 86     Button_2(k);
 87 
 88     for (int i = 0; i < n; i++) x[i] ^= 1;
 89     Button_2(k + 1);
 90     for (int i = 0; i < n; i++) x[i] ^= 1;
 91 }
 92 
 93 int main()
 94 {
 95     freopen("lamps.in""r", stdin);
 96     freopen("lamps.out""w", stdout);
 97 
 98     cin >> n >> c;
 99 
100     int t;
101     while (true)
102     {
103         cin >> t;
104         if (t == -1break;
105         FinalStatus[t - 1= ON;
106     }
107     while (true)
108     {
109         cin >> t;
110         if (t == -1break;
111         FinalStatus[t - 1= OFF;
112     }
113 
114     if (c == 0)
115     {
116         int i;
117         for (i = 0; i < n; i++)
118             if (FinalStatus[i])
119                 break;
120         if (i == n)
121         {
122             for (int i = 0; i < n; i++)
123                 cout << 1;
124             cout << endl;
125         }
126         else
127             cout << "IMPOSSIBLE" << endl;
128         exit(0);
129     }
130 
131     //=========================
132     for (int i = 0; i < n; i++)
133         x[i] = true;
134 
135     Button_1(0);
136 
137     sort(ans, ans + ans_cnt);
138     for (int i = 0; i < ans_cnt; i++)
139         cout << ans[i] << endl;
140 
141     if (ans_cnt == 0)
142         cout << "IMPOSSIBLE" << endl;
143 
144     return 0;
145 }
146 

posted @ 2009-03-30 19:00 superman 阅读(108) | 评论 (0)编辑 收藏

 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 bool isRunaroundNumber(int num)
 6 {
 7     int n = 0;
 8     int t, x[10], c[10= { 0 };
 9 
10     while (num)
11     {
12         t = num % 10;
13 
14         if ((c[t] += 1> 1)
15             return false;
16 
17         x[n++= t;
18         num /= 10;
19     }
20 
21     for (int i = 0; i < n / 2; i++)
22         swap(x[i], x[n - i - 1]);
23 
24     //=============================
25     memset(c, 0sizeof(c));
26 
27     int p = 0;
28     for (int i = 0; i < n; i++)
29     {
30         c[p]++;
31         if (c[p] > 1)
32             return false;
33         p = (p + x[p]) % n;
34     }
35 
36     return p == 0;
37 }
38 
39 int main()
40 {
41     freopen("runround.in""r", stdin);
42     freopen("runround.out""w", stdout);
43 
44     int m;
45 
46     cin >> m;
47     for (int i = m + 1true; i++)
48         if (isRunaroundNumber(i))
49         {
50             cout << i << endl;
51             break;
52         }
53 
54     return 0;
55 }
56 

posted @ 2009-03-30 17:23 superman 阅读(90) | 评论 (0)编辑 收藏

 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     freopen("subset.in""r", stdin);
 8     freopen("subset.out""w", stdout);
 9 
10     int n, m;
11 
12     cin >> n;
13 
14     m = (1 + n) * n / 2;
15     if (m % 2 == 1)
16     {
17         cout << 0 << endl;
18         exit(0);
19     }
20     else
21         m /= 2;
22 
23     unsigned int cnt[400= { 1 };
24     for (int i = 1; i <= n; i++)
25         for (int j = m; j >= i; j--)
26             cnt[j] += cnt[j - i];
27 
28     cout << cnt[m] / 2 << endl;
29 
30     return 0;
31 }
32 

posted @ 2009-03-30 13:39 superman 阅读(143) | 评论 (0)编辑 收藏

 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     freopen("preface.in""r", stdin);
 8     freopen("preface.out""w", stdout);
 9 
10     int n;
11     cin >> n;
12 
13     int I, V, X, L, C, D, M;
14     I = V = X = L = C = D = M = 0;
15 
16     while (n)
17     {
18         int c = n--;
19         while (c)
20         {
21             if (c >= 1000) { c -= 1000, M++     ; continue; }
22             if (c >= 900 ) { c -= 900 , C++, M++continue; }
23             if (c >= 500 ) { c -= 500 , D++     ; continue; }
24             if (c >= 400 ) { c -= 400 , C++, D++continue; }
25             if (c >= 100 ) { c -= 100 , C++     ; continue; }
26             if (c >= 90  ) { c -= 90  , X++, C++continue; }
27             if (c >= 50  ) { c -= 50  , L++     ; continue; }
28             if (c >= 40  ) { c -= 40  , X++, L++continue; }
29             if (c >= 10  ) { c -= 10  , X++     ; continue; }
30             if (c >= 9   ) { c -= 9   , I++, X++continue; }
31             if (c >= 5   ) { c -= 5   , V++     ; continue; }
32             if (c >= 4   ) { c -= 4   , I++, V++continue; }
33             if (c >= 1   ) { c -= 1   , I++     ; continue; }
34         }
35     }
36 
37     if (I) cout << 'I' << ' ' << I << endl;
38     if (V) cout << 'V' << ' ' << V << endl;
39     if (X) cout << 'X' << ' ' << X << endl;
40     if (L) cout << 'L' << ' ' << L << endl;
41     if (C) cout << 'C' << ' ' << C << endl;
42     if (D) cout << 'D' << ' ' << D << endl;
43     if (M) cout << 'M' << ' ' << M << endl;
44 
45     return 0;
46 }
47 

posted @ 2009-03-28 17:10 superman 阅读(88) | 评论 (0)编辑 收藏

仅列出标题
共19页: 1 2 3 4 5 6 7 8 9 Last