USACO Snail Trails 解题报告
2014-09-30 22:40
369 查看
这道题就是一道DFS搜索题,我是按照自己习惯的方式进行的,进去之后就mark,出来前unmark。主要是需要看清题,即如果能走的话一直往前走,否则,需要看是否遇到障碍了,如果遇到的是之前走过的点就只能停了。前者的情况可以左转或右转。所以这道题我也保留了方向。
/* ID: thestor1 LANG: C++ TASK: snail */ #include <iostream> #include <fstream> #include <cmath> #include <cstdio> #include <cstring> #include <climits> #include <cassert> #include <string> #include <vector> #include <set> #include <map> #include <queue> #include <stack> #include <algorithm> using namespace std; bool isIn(int r, int c, int N) { return 0 <= r && r < N && 0 <= c && c < N; } void search(vector<std::vector<int> > &grid, int r, int c, int d, int cnt, int &largest) { // cout<<"[debug]r: "<<r<<", c: "<<c<<endl; if (cnt > largest) { largest = cnt; // cout<<"[debug]largest: "<<largest<<endl; } grid[r][c] = 1; int N = grid.size(); int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1}; int nr = r + dx[d], nc = c + dy[d]; if (isIn(nr, nc, N) && !grid[nr][nc]) { search(grid, nr, nc, d, cnt + 1, largest); } else if (!isIn(nr, nc, N) || grid[nr][nc] == 2) { int turn[] = {-1, 1}; for (int t = 0; t < 2; ++t) { int nd = (d + 4 + turn[t]) % 4; nr = r + dx[nd]; nc = c + dy[nd]; if (isIn(nr, nc, N) && !grid[nr][nc]) { search(grid, nr, nc, nd, cnt + 1, largest); } } } grid[r][c] = 0; } int main() { ifstream fin("snail.in"); ofstream fout("snail.out"); int N, B; fin>>N>>B; std::vector<std::vector<int> > grid(N, std::vector<int>(N, 0)); for (int i = 0; i < B; ++i) { char c; int r; fin>>c>>r; // cout<<"r: "<<r<<", c: "<<c<<endl; grid[r - 1][c - 'A'] = 2; } grid[0][0] = 1; // for (int r = 0; r < N; ++r) // { // for (int c = 0; c < N; ++c) // { // if (grid[r][c]) // { // cout<<'#'; // } // else // { // cout<<'.'; // } // } // cout<<endl; // } int largest = 0; search(grid, 0, 0, 1, 1, largest); // cout<<"[debug]after first search: "<<endl; // for (int r = 0; r < N; ++r) // { // for (int c = 0; c < N; ++c) // { // if (grid[r][c]) // { // cout<<'#'; // } // else // { // cout<<'.'; // } // } // cout<<endl; // } search(grid, 0, 0, 2, 1, largest); fout<<largest<<endl; fin.close(); fout.close(); return 0; }
相关文章推荐
- USACO Pollutant Control 解题报告
- USACO Musical Themes 解题报告
- usaco Transformations 解题报告
- usaco Calf Flac 解题报告
- usaco problem76:mixing milk解题报告
- USACO :Subset Sums 解题报告
- USACO A Game 解题报告
- USACO Section1.2 Name That Number 解题报告
- USACO Section2.1 Healthy Holsteins 解题报告 【icedream61】
- USACO 2.3.2 Cow Pedigrees 解题报告
- USACO Section 2.1 The Castle 解题报告
- USACO Frame Up 解题报告
- USACO Section2.3 Longest Prefix 解题报告 【icedream61】
- USACO4.3.3 街道赛跑 解题报告
- USACO Combination Lock 解题报告
- USACO Milk Measuring 解题报告
- USACO Twofive 解题报告
- USACO :Runaround Numbers 解题报告
- 近期刷USACO金组简化版解题报告
- USACO 5.3解题报告