求助(必关)

P1767 家族

@[Danny_chan](/user/1032960) 那道题啊
by mc_him @ 2024-02-26 19:46:59


https://www.luogu.com.cn/discuss/781175
by c20220625 @ 2024-02-26 19:49:04


```cpp #include<bits/stdc++.h> using namespace std; int n, cnt; string s; int l[505], bk[505][505]; int dx[4] = {0, 1, 0, -1}; int dy[4] = {1, 0, -1, 0}; void dfs(int x, int y) { if (x < 1 || x > n || y < 1 || y > l[x] || !bk[x][y]) return; bk[x][y] = 0; for (int k = 0; k <= 3; k++) dfs(x + dx[k], y + dy[k]); return; } int main() { cin >> n; getline(cin, s); for (int i = 1; i <= n; i++) { getline(cin, s); l[i] = s.length(); for (int j = 1; j <= l[i]; j++) { if (s[j - 1] >= 'a' && s[j - 1] <= 'z') bk[i][j] = 1; } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= l[i]; j++) { if (bk[i][j]) { dfs(i, j); cnt++; } } } cout << cnt; return 0; } @[Danny_chan](/user/1032960) 顺着你的思路写了一个代码
by OJ_killer @ 2024-02-26 20:46:13


```cpp #include<iostream> using namespace std; int n; string s[1010]; bool f[1010][1010]; int sum=0,line=1; int maxx=-1e9; int dx[4]={-1,0,1,0}; int dy[4]={0,1,0,-1}; int l[1010]; void dfs(int x,int y){ for(int i=0;i<4;i++){ int x1=x+dx[i]; int y1=y+dy[i]; if(x1<=0||x1>n||y1<=0||y1>l[x]) continue;//maxx换为当前行的长度,每行长度不一 if(s[x1][y1]<'a'||s[x1][y1]>'z') continue; if(f[x1][y1]==true) continue; f[x1][y1]=true; dfs(x1,y1); } } int main(){ cin>>n; for(int i=0;i<=n;i++){//"i=1"改为"i=0",把cin的换行符吃掉,否则会少输入一行 getline(cin,s[i]); l[i]=s[i].size(); //计算当前行长度 } for(int i=1;i<=n;i++){ for(int j=1;j<=l[i];j++){ if(s[i][j]>='a'&&s[i][j]<='z'&&f[i][j]==false){ dfs(i,j); sum++; } } } cout<<sum; return 0; } ```
by czy0407 @ 2024-02-26 20:48:35


@[Danny_chan](/user/1032960) 明显是因为没判断数据末尾的字符 `\r`。
by angiing1222 @ 2024-02-26 21:04:55


谢谢各位大佬,已关
by Danny_chan @ 2024-02-26 21:14:01


|