当前位置:网站首页>[search] connectivity model of DFS + search order
[search] connectivity model of DFS + search order
2022-07-27 04:57:00 【Xuanche_】

AcWing 1112. maze
sample input :
2 3 .## ..# #.. 0 0 2 2 5 ..... ###.# ..#.. ###.. ...#. 0 0 4 0sample output :
YES NO
#include <iostream> #include <algorithm> #include <cstring> using namespace std; const int N = 110; int n; char g[N][N]; bool st[N][N]; int xa, ya, xb, yb; int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1}; bool dfs(int x, int y) { if(g[x][y] == '#') return false; if(x == xb && y == yb) return true; st[x][y] = true; for(int i = 0; i < 4; i ++ ) { int a = x + dx[i], b = y + dy[i]; if(a < 0 || a >= n || b < 0 || b >= n) continue; if(st[a][b]) continue; if(g[a][b] == '#') continue; if(dfs(a, b)) return true; } return false; } int main() { int T; cin >> T; while(T -- ) { cin >> n; for(int i = 0; i < n; i ++ ) cin >> g[i]; memset(st, 0, sizeof st); cin >> xa >> ya >> xb >> yb; if(dfs(xa, ya)) puts("YES"); else puts("NO"); } return 0; }
AcWing 1113. Red and black
sample input :
6 9 ....#. .....# ...... ...... ...... ...... ...... #@...# .#..#. 0 0sample output :
45
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 25;
int n, m;
char g[N][N];
bool st[N][N];
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
int dfs(int x, int y)
{
int cnt = 1;
st[x][y] = true;
for(int i = 0; i < 4; i ++ )
{
int a = x + dx[i], b = y + dy[i];
if(a < 0 || a >= n || b < 0 || b >= m) continue;
if(g[a][b] != '.') continue;
if(st[a][b]) continue;
cnt += dfs(a, b);
}
return cnt;
}
int main()
{
while(cin >> m >> n, m || n)
{
for(int i = 0; i < n; i ++ ) cin >> g[i];
int x, y;
for(int i = 0; i < n; i ++ )
for(int j = 0; j < m; j ++ )
if(g[i][j] == '@')
{
x = i;
y = j;
}
memset(st, 0, sizeof st);
cout << dfs(x, y) << endl;
}
return 0;
}AcWing 1116. Horse walking day
sample input :
1 5 4 0 0sample output :
32
#include <cstring> #include <iostream> #include <algorithm> using namespace std; const int N = 10; int n, m; bool st[N][N]; int ans; int dx[8] = {-2, -1, 1, 2, 2, 1, -1, -2}; int dy[8] = {1, 2, 2, 1, -1, -2, -2, -1}; void dfs(int x, int y, int cnt) { if (cnt == n * m) { ans ++ ; return; } st[x][y] = true; for (int i = 0; i < 8; i ++ ) { int a = x + dx[i], b = y + dy[i]; if (a < 0 || a >= n || b < 0 || b >= m) continue; if (st[a][b]) continue; dfs(a, b, cnt + 1); } st[x][y] = false; } int main() { int T; scanf("%d", &T); while (T -- ) { int x, y; scanf("%d%d%d%d", &n, &m, &x, &y); memset(st, 0, sizeof st); ans = 0; dfs(x, y, 1); printf("%d\n", ans); } return 0; }
AcWing 1117. The word chain
sample input :
5 at touch cheat choose tact asample output :
23Tips
Connected “ dragon ” by atoucheatactactouchoose.
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 21;
int n;
string word[N];
int g[N][N];
int used[N];
int ans;
void dfs(string dragon, int last)
{
ans = max((int)dragon.size(), ans);
used[last] ++ ;
for (int i = 0; i < n; i ++ )
if (g[last][i] && used[i] < 2)
dfs(dragon + word[i].substr(g[last][i]), i);
used[last] -- ;
}
int main()
{
cin >> n;
for (int i = 0; i < n; i ++ ) cin >> word[i];
char start;
cin >> start;
for (int i = 0; i < n; i ++ )
for (int j = 0; j < n; j ++ )
{
string a = word[i], b = word[j];
for (int k = 1; k < min(a.size(), b.size()); k ++ )
if (a.substr(a.size() - k, k) == b.substr(0, k))
{
g[i][j] = k;
break;
}
}
for (int i = 0; i < n; i ++ )
if (word[i][0] == start)
dfs(word[i], i);
cout << ans << endl;
return 0;
}
AcWing 1118. Divided into coprime groups
sample input :
6 14 20 33 117 143 175sample output :
3
- Add a number to the last group
- Open a new group
according to Order of Subscripts successively Add all the numbers to the group in
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 10;
int n;
int p[N];
int group[N][N];
bool st[N];
int ans = N;
int gcd(int a, int b)
{
return b ? gcd(b, a % b) : a;
}
bool check(int group[], int gc, int i)
{
for (int j = 0; j < gc; j ++ )
if (gcd(p[group[j]], p[i]) > 1)
return false;
return true;
}
void dfs(int g, int gc, int tc, int start)
{
if (g >= ans) return;
if (tc == n) ans = g;
bool flag = true;
for (int i = start; i < n; i ++ )
if (!st[i] && check(group[g], gc, i))
{
st[i] = true;
group[g][gc] = i;
dfs(g, gc + 1, tc + 1, i + 1);
st[i] = false;
flag = false;
}
if (flag) dfs(g + 1, 0, tc, 0);
}
int main()
{
cin >> n;
for (int i = 0; i < n; i ++ ) cin >> p[i];
dfs(1, 0, 0, 0);
cout << ans << endl;
return 0;
}边栏推荐
- Scala immutable map, variable map, map conversion to other data types
- Shell programming enhancements
- 2019强网杯upload
- 会议OA之我的审批
- JS第二天(变量、变量的使用、命名规则、语法扩展)
- Easy to use mobile app automation testing framework where to find? Just collect this list!
- Photoshop各历史版本回顾以及系统要求
- 单元测试chapter6
- 使用Unity做一个艺术字系统
- Structural mode - bridging mode
猜你喜欢

会议OA之我的审批

Dino paper accuracy, and analyze the variant of its model structure & Detr

STL upper series - detailed explanation of list container

Photoshop裁剪工具隐藏技巧

Interview must ask | what stages does a thread go through from creation to extinction?

C language - two dimensional array, pointer

「Photoshop2021入门教程」对齐与分布制作波点图案

Redis interview question (2022)

Plato Farm有望通过Elephant Swap,进一步向外拓展生态

ceph操作
随机推荐
JS day 2 (variables, variable usage, naming rules, syntax extensions)
HCIA dynamic routing rip basic experiment
数字中国建设峰会闭幕,现场海量图片一览!
Final Cut Pro中文教程 (2) 素材窗口的认识
如何做数据平滑迁移:双写方案
Horizon sunrise X3 PI (IV) running on board (unfinished)
iPhone13再降价,其实只是做做样子,消费者都在等iPhone14
STL upper series - detailed explanation of list container
[dynamic planning hundred questions strengthening plan] 11~20 (continuously updating)
【AtCoder Beginner Contest 260 (A·B·C)】
「Photoshop2021入门教程」对齐与分布制作波点图案
干货 | 独立站运营怎么提高在线聊天客户服务?
Solution: read the files with different names in the two folders and deal with the files with different mappings
【AtCoder Beginner Contest 260 (A·B·C)】
【Acwing】第61场周赛 题解
Full revolutionary networks for semantic segmentation (FCN)
Bo Yun container cloud and Devops platform won the trusted cloud "technology best practice Award"
「Photoshop2021入门教程」调整图片到不同的长宽比
The price reduction of iphone13 is just a show. Consumers are waiting for iphone14
使用Unity做一个艺术字系统




