This commit is contained in:
e2hang
2025-09-16 23:10:48 +08:00
parent e9519e8558
commit 24522486f1
10 changed files with 249 additions and 0 deletions

View File

@@ -1,66 +0,0 @@
#include <iostream>
#include <utility>
#include <deque>
using namespace std;
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
int dx[4] = {0, 0, -1, 1};
int dy[4] = {1, -1, 0, 0};
int dxx[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
int dyy[8] = {1, 0, -1, 1, -1, 1, 0, -1};
int main(){
deque<pair<int, int>> q;
int n, m;
//<2F><><EFBFBD><EFBFBD>
cin >> n >> m;
int startx, starty;
//<2F><><EFBFBD><EFBFBD>
cin >> startx >> starty;
deque<deque<int>> maze;
deque<deque<bool>> visited;
deque<deque<int>> dist(n, deque<int>(m, 0)); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
maze.resize(n);
visited.resize(n, deque<bool>(m, false));
int tmp;
//0<><30>ʾǽ<CABE><C7BD>1<EFBFBD><31>ʾ·
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
cin >> tmp;
maze[i].push_back(tmp);
}
}
tmp = 0;
q.push_back(make_pair(startx, starty));
while(!q.empty()){
pair<int, int> u = q.front();
q.pop_front();
int x = u.first, y = u.second;
visited[x][y] = true;
for(int i = 0; i < 4; i++){
if(x + dx[i] >= 0 && x + dx[i] < n && y + dy[i] >= 0 && y + dy[i] < m && !visited[x + dx[i]][y + dy[i]] && maze[x + dx[i]][y + dy[i]] == 1){
visited[x + dx[i]][y + dy[i]] = true;
dist[x + dx[i]][y + dy[i]] = dist[x][y] + 1; // <20><><EFBFBD>¾<EFBFBD><C2BE><EFBFBD>
q.push_back(make_pair(x + dx[i], y + dy[i]));
}
}
}
cout << endl;
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
cout << dist[i][j] << " ";
}
cout << endl;
}
return 0;
}
/*
6 5
0 0
1 1 0 1 1
1 0 1 1 1
1 0 1 0 0
1 0 1 1 1
1 1 1 0 1
1 1 1 1 1
*/

View File

@@ -1,63 +0,0 @@
#include <iostream>
#include <vector>
#include <set>
#include <bits/stdc++.h>
using namespace std;
bool have = false;
set<tuple<int,int,int>> visited;
int A, B, C; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void dfs(int a, int b, int c, int target) {
if (a == target || b == target || c == target) {
have = true;
return;
}
auto state = make_tuple(a, b, c);
if (visited.find(state) != visited.end()) return;
visited.insert(state);
// <20><><EFBFBD>ֵ<EFBFBD>ˮ<EFBFBD><CBAE><EFBFBD><EFBFBD>
// a -> b
{
int pour = min(a, B - b);
dfs(a - pour, b + pour, c, target);
}
// a -> c
{
int pour = min(a, C - c);
dfs(a - pour, b, c + pour, target);
}
// b -> a
{
int pour = min(b, A - a);
dfs(a + pour, b - pour, c, target);
}
// b -> c
{
int pour = min(b, C - c);
dfs(a, b - pour, c + pour, target);
}
// c -> a
{
int pour = min(c, A - a);
dfs(a + pour, b, c - pour, target);
}
// c -> b
{
int pour = min(c, B - b);
dfs(a, b + pour, c - pour, target);
}
}
int main() {
int target;
cin >> A >> B >> C >> target;
dfs(A, 0, 0, target); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC> A װ<><D7B0>
if (have) cout << "YES" << endl;
else cout << "NO" << endl;
return 0;
}

View File

@@ -1,32 +0,0 @@
#include <iostream>
using namespace std;
//ab<61><62><EFBFBD>յ<EFBFBD>
int dx[8] = {-2, -1, 1, 2, 2, 1, -1, -2};
int dy[8] = {1, 2, 2, 1, -1, -2, -2, -1};
int step[9][9];
void dfs(int _a, int _b, int _n, int _m){
if(_n > 8 || _n < 1 || _m < 1 || _m > 8) return;
if(_n == _a && _m == _b) return;
for(int i = 0; i < 8; i++){
int nx = _n + dx[i];
int ny = _m + dy[i];
if (nx >= 1 && nx <= 8 && ny >= 1 && ny <= 8) {
int stepnew = step[_n][_m] + 1;
if (step[nx][ny] == 0 || stepnew < step[nx][ny]) {
step[nx][ny] = stepnew;
dfs(_a, _b, nx, ny);
}
}
}
}
int main(){
int a, b, n, m;
cin >> n >> m >> a >> b;
step[n][m] = 0;
dfs(a, b, n, m);
cout << step[a][b] << endl;
return 0;
}

View File

@@ -1,51 +0,0 @@
#include <iostream>
#include <vector>
using namespace std;
int m = 5, n = 5;
vector<vector<char>> s;
vector<vector<bool>> visited;
vector<vector<int>> ids;
void dfs(int x, int y, int id){
if(x < 0 || x >= m || y < 0 || y >=n) return;
if(s[x][y] != '@') return;
if(visited[x][y] == true) return;
visited[x][y] = true;
ids[x][y] = id;
dfs(x - 1, y - 1, id + 1);
dfs(x - 1, y, id + 1);
dfs(x - 1, y + 1, id + 1);
dfs(x, y - 1, id + 1);
dfs(x, y + 1, id + 1);
dfs(x + 1, y - 1, id + 1);
dfs(x + 1, y, id + 1);
dfs(x + 1, y + 1, id + 1);
cout << id << " ";
}
int main(){
s.resize(m);
visited.assign(m, vector<bool>(n, false));
ids.assign(m, vector<int>(n, 0));
char tmp;
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
cin >> tmp;
s[i].push_back(tmp);
}
}
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
if(!visited[i][j] && s[i][j] == '@') dfs(i, j, 1);
}
}
cout << endl;
int max = -1;
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
if(ids[i][j] > max) max = ids[i][j];
}
}
cout << max << endl;
}

View File

@@ -1,40 +0,0 @@
#include <iostream>
#include <string>
using namespace std;
//确定每个节点要干什么
bool dfs(const string& s, int l, int r) {
if (l > r) return true; // 空串平衡
if (l == r) return false; // 单个字符不平衡
// 必须匹配类型
char open = s[l];
char close = (open == '(') ? ')' : (open == '[') ? ']' : 0;
if (close == 0) return false; // 非括号字符
int cnt = 0;
int p = -1;
for (int i = l; i <= r; i++) {
if (s[i] == '(' || s[i] == '[') cnt++;
if (s[i] == ')' || s[i] == ']') cnt--;
if (cnt == 0) {
p = i; // 找到匹配的右括号
break;
}
}
if (p == -1) return false; // 没找到匹配
if (s[p] != close) return false; // 类型不匹配(第一个和最后一个字符匹配)
// 内部和剩余区间递归
return dfs(s, l + 1, p - 1) && dfs(s, p + 1, r);
}
int main(){
string a;
while(true)
{
cin >> a;
dfs(a, 0, a.size() - 1) ? cout << "YES" << endl : cout << "NO" << endl;
}
return 0;
}

View File

@@ -1,80 +0,0 @@
#include <iostream>
#include <vector>
#include <deque>
#include <string>
using namespace std;
struct Graph{
int v;
int e;
vector<vector<int>> adj;
vector<bool> visited;
Graph(int _v) : v(_v) {
adj.resize(v);
visited.assign(v, false);
}
bool validateV(int _v){
return ( _v >= 0 && _v < v);
}
void insert(int _a, int _b){
if(!validateV(_a) || !validateV(_b)) return;
adj[_a].push_back(_b);
}
int degree(int _v){
if(!validateV(_v)) return 0;
return outDegree(_v) + inDegree(_v);
}
int outDegree(int _v){
if(!validateV(_v)) return 0;
return adj[_v].size();
}
int inDegree(int _v){
if(!validateV(_v)) return 0;
int js = 0;
for(auto x : adj){
for(int i = 0; i < x.size(); i++){
if(x[i] == _v) js++;
}
}
return js;
}
void dfs(int _v){
if(!validateV(_v)) return;
if(visited[_v]) return;
visited[_v] = true;
for(auto x : adj[_v]){
dfs(x);
}
}
bool isConnected(){
dfs(0);
for(auto x : visited){
if(!x) return false;
}
int js = 0;
for(int i = 0; i < v; i++){
if(degree(i) % 2 == 1) js++;
}
return (js == 0) || (js == 2);
}
};
int main(){
int n;
cin >> n;
Graph g(n);
vector<string> s;
s.resize(n);
for(int i = 0; i < n; i++){
cin >> s[i];
}
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(i != j && s[i].back() == s[j].front()) g.insert(i, j);
}
}
g.isConnected() ? cout << "YES" << endl : cout << "NO" << endl;
system("pause");
return 0;
}