赞
踩
#include <algorithm> #include <iostream> #include <string> #include <vector> using namespace std; int main() { int m, n; cin >> m; cin >> n; char arr[n]; for (int i = 0; i < n; ++i) { cin >> arr[i]; } // int m = 5, n = 6; // char arr[n] = "ABABAA"; //以上是输入 m 是芯片数,n是业务数,arr就是一个字符数组,放了一串ABABABAB char temp; int b[m]; for (int i = 0; i < m; ++i) { b[i] = 0; } int a1 = 0; int index1 = 1; //未满状态的第一块芯片编号 int index2 = 1; //全空状态的第一块芯片编号 for (int i = 0; i < n - 1; ++i) { temp = arr[i]; if (temp == 'A') { if (a1 + 1 == 4) { a1 = 0; index1 = index2; } else { a1++; if (index1 == index2) { index2++; } } } else if (temp == 'B') { if (index1 == index2) { index1++; index2++; } else { index2++; } } } // 判断最后一个 if (arr[n - 1] == 'A') { if (index1 > m) { cout << 0 << endl; cout << 0 << endl; } else { cout << index1 << endl; cout << a1 + 1 << endl; } } else { if (index2 > m) { cout << 0 << endl; cout << 0 << endl; } else { cout << index2 << endl; cout << 1 << endl; } } return 0; }
上dfs
#include <algorithm> #include <iostream> #include <string> using namespace std; //全局变量 const int MAX_X = 100; const int MAX_Y = 100; int min_step = 10000; int number = 0; int my_x, my_y; int map[MAX_X][MAX_Y]; int book[MAX_X][MAX_Y]; int start_x, start_y; int dest_x, dest_y; void dfs(int x, int y, int step) { /*up, right, down, left*/ int next[4][2] = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}}; int tx, ty; if (x == dest_x && y == dest_y) { if (step == min_step) { number++; } if (step < min_step) { min_step = step; number = 1; } return; } for (int i = 0; i < 4; i++) { tx = x + next[i][0]; ty = y + next[i][1]; if (tx > my_x || ty > my_y || tx < 0 || ty < 0) continue; if (map[tx][ty] == 0 && book[tx][ty] == 0) { book[tx][ty] = 1; dfs(tx, ty, step + 1); book[tx][ty] = 0; } } } int main() { cin >> my_x >> my_y; cin >> start_x >> start_y; cin >> dest_x >> dest_y; int num; cin >> num; //建一张二维表,初始化权为0 int x, y; //湖泊的位置 for (int i = 0; i < num; ++i) { //高山湖泊用1代表 cin >> x >> y; map[x][y] = 1; } book[start_x][start_y] = 1; dfs(start_x, start_y, 0); cout << number << " " << min_step << endl; return 0; }
652. 寻找重复的子树 差不多的题,只要把652的结果里面最长的那个返回就行了
万万没想到他给了一个层序遍历的数组,我不知道怎么变成一棵树,裂开,没做出来
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。