• 搜索入门


    记录点滴。

      1 /*
      2 2015.6    HT
      3 ACM Work_9
      4 
      5 */
      6 
      7 #include <iostream>
      8 #include <algorithm>
      9 #include <cstdio>
     10 using namespace std;
     11 
     12 
     13 /*
     14 Can you find it
     15 
     16 Ai+Bj+Ck = X
     17 */
     18 //int a[505], b[505], c[505];
     19 //int sum[505 * 505];
     20 //int l, n, m, k;
     21 //int flag;
     22 //
     23 //// 二分查找
     24 //void binary(int x)
     25 //{
     26 //    int left, right, mid;
     27 //    left = 0;
     28 //    right = k - 1;
     29 //    while (left <= right)
     30 //    {
     31 //        mid = (left + right) / 2;
     32 //        if (sum[mid] > x)
     33 //            right = mid - 1;
     34 //        else if (sum[mid]<x)
     35 //            left = mid + 1;
     36 //        else
     37 //        {
     38 //            flag = 1;
     39 //            return;
     40 //        }
     41 //    }
     42 //    return;
     43 //}
     44 //
     45 //int main()
     46 //{
     47 //    int i, j, q, x, count = 1;
     48 //    while (cin >> l >> n >> m)
     49 //    {
     50 //        for (i = 0; i<l; i++)
     51 //            cin >> a[i];
     52 //        for (i = 0; i<n; i++)
     53 //            cin >> b[i];
     54 //        for (i = 0; i<m; i++)
     55 //            cin >> c[i];
     56 //
     57 //        k = 0;
     58 //        for (i = 0; i<l; i++)
     59 //        for (j = 0; j<n; j++)
     60 //        {
     61 //            sum[k++] = a[i] + b[j];
     62 //        }
     63 //        sort(sum, sum + k);
     64 //
     65 //        cin >> q;
     66 //        cout << "Case " << count++ << ":" << endl;
     67 //
     68 //        while (q--)
     69 //        {
     70 //            cin >> x;
     71 //            flag = 0;
     72 //            for (i = 0; i<m; i++)
     73 //            {
     74 //                binary(x - c[i]);
     75 //                if (flag)
     76 //                {
     77 //                    cout << "YES" << endl;
     78 //                    break;
     79 //                }
     80 //            }
     81 //            if (!flag)
     82 //                cout << "NO" << endl;
     83 //        }
     84 //    }
     85 //    return 0;
     86 //}
     87 
     88 
     89 
     90 /*
     91 Strange fuction
     92 
     93 F(x) = 6*x^7+8*x^6+7*x^3+5*x^2-y*x (0 <= x <=100)
     94 求其给定范围内的最小值
     95 一阶导F'(x) = 42*x^6+48*x^5+21*x^2+10*x-y
     96 无论y取什么值,总可以找到x1∈[0, 100]使F'(x1)=0,x∈[0, x1]时,函数递减
     97 x∈[x1, 100]时,函数递增。所以F(x1)便是极小值,也是最小值
     98 */
     99 //const double eps = 1e-6;
    100 //int t;
    101 //double y;
    102 //double fun(double x)
    103 //{
    104 //    return 6 * pow(x, 7) + 8 * pow(x, 6) + 7 * pow(x, 3) + 5 * pow(x, 2) - y*x;
    105 //}
    106 //
    107 //double func(double x)
    108 //{
    109 //    return 42 * pow(x, 6) + 48 * pow(x, 5) + 21 * pow(x, 2) + 10 * x - y;
    110 //}
    111 //
    112 //int main()
    113 //{ 
    114 //    scanf_s("%d", &t);
    115 //    while (t--)
    116 //    {
    117 //        scanf_s("%lf", &y);
    118 //        double ll = 0, rr = 1e2, mid;
    119 //        while (ll + eps <= rr)
    120 //        {
    121 //            mid = (ll + rr) / 2;
    122 //            if (func(mid)>0)
    123 //                rr = mid;
    124 //            else 
    125 //                ll = mid;
    126 //        }
    127 //        printf_s("%.4f
    ", fun(mid));
    128 //    }
    129 //    return 0;
    130 //}
    131 
    132 
    133 
    134 /*
    135 Tempter of the Bone:
    136 
    137 奇偶性剪枝:
    138 可以把map看成这样:
    139 0 1 0 1 0 1
    140 1 0 1 0 1 0
    141 0 1 0 1 0 1
    142 1 0 1 0 1 0
    143 0 1 0 1 0 1
    144 从为 0 的格子走一步,必然走向为 1 的格子
    145 从为 1 的格子走一步,必然走向为 0 的格子
    146 即:
    147 0 ->1或1->0 必然是奇数步
    148 0->0 走1->1 必然是偶数步
    149 
    150 结论:
    151 所以当遇到从 0 走向 0 但是要求时间是奇数的,或者从 1 走向 0 但是要求时间是偶数的 都可以直接判断不可达!
    152 
    153 X: 墙壁
    154 S: 开始位置
    155 D: 迷宫的门
    156 .: 空的方格
    157 
    158 */
    159 char map[101][101];
    160 int n, m, fx, fy;
    161 bool isok;
    162 
    163 void dfs(int x, int y, int time)
    164 {
    165     if (isok == 1)
    166         return;
    167 
    168     // 是否越界
    169     if (x<0 || y<0 || x > n - 1 || y > m - 1 || time<0 || map[x][y] == 'X')
    170         return;
    171 
    172     // 减枝  最短步数减枝  奇偶减枝
    173     if (time < abs(fx - x) + abs(fy - y) || (time - (abs(fx - x) + abs(fy - y))) % 2)
    174         return;
    175 
    176     // 是否到了终点
    177     else if (time == 0 && (fx == x && fy == y))
    178     {
    179         isok = true;
    180         return;
    181     }
    182 
    183     // 四个方向遍历
    184     else
    185     {
    186         map[x][y] = 'X';
    187         dfs(x - 1, y, time - 1);
    188         map[x][y] = '.';
    189 
    190         map[x][y] = 'X';
    191         dfs(x + 1, y, time - 1);
    192         map[x][y] = '.';
    193 
    194         map[x][y] = 'X';
    195         dfs(x, y - 1, time - 1);
    196         map[x][y] = '.';
    197 
    198         map[x][y] = 'X';
    199         dfs(x, y + 1, time - 1);
    200         map[x][y] = '.';
    201     }
    202     return;
    203 
    204 }
    205 
    206 int main()
    207 {
    208     int i, j, sx, sy, t;
    209 
    210     while (cin >> n >> m >> t)
    211     {
    212         if (n == 0 && m == 0 && t == 0)
    213             break;
    214 
    215         for (i = 0; i<n; ++i)
    216         for (j = 0; j<m; ++j)
    217         {
    218             cin >> map[i][j];
    219             if (map[i][j] == 'S')
    220             {
    221                 sx = i;
    222                 sy = j;
    223             }
    224             else if (map[i][j] == 'D')
    225             {
    226                 fx = i;
    227                 fy = j;
    228             }
    229         }
    230 
    231         isok = false;
    232         dfs(sx, sy, t);
    233         if (isok == 1)
    234             cout << "YES" << endl;
    235         else
    236             cout << "NO" << endl;
    237     }
    238 
    239     return 0;
    240 }
  • 相关阅读:
    计算机专业术语中英对照
    PhpStorm如何下载github上的代码到本地
    PDO学习
    Shell中特殊的变量
    Shell中变量的使用
    修改cmd的字体
    Shell的输入输出
    Shell入门第一课
    设计模式--观察者(Observer)
    eclipse中使用git提交代码到github
  • 原文地址:https://www.cnblogs.com/ht-beyond/p/4573096.html
Copyright © 2020-2023  润新知