求助,第17个测试点RE
查看原帖
求助,第17个测试点RE
437005
xiaoqi666楼主2023/1/13 22:53

代码如下


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;

public class Main {
	static BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
	static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
	static PrintWriter cout = new PrintWriter(bw);
	static StreamTokenizer st = new StreamTokenizer(buf);
	static int nextInt() throws IOException {
		st.nextToken();
		return (int)st.nval;
	}
	static int n,m,cnt = 0;
	static int a[][] = new int[1001][1001];
	static int f[][] = new int[1001][1001];
	static int dx[] = {0,-1,-1,-1,0,1,1,1},dy[] = {-1,-1,0,1,1,1,0,-1};
	static void dfs(int x,int y)
	{
		if( f[x][y] == 1 || a[x][y] != 2) return ;
		cnt ++;
		f[x][y] = 1;
		//a[x][y] = -1;
		for(int i = 0;i < 8;i ++)
		{
			int x1 = x + dx[i],y1 = y + dy[i];
			if(x1 < 0 || x1 >= n || y1 < 0 || y1 >= m || f[x1][y1] == 1) continue;
			else
			{
				cnt ++;
				f[x1][y1] = 1;
				//a[x1][y1] = -1;
				//cout.println(x1 + " " + y1);
				dfs(x1,y1);
			}
		}
	}
	public static void main(String[] args) throws IOException {
		n = nextInt();
		m = nextInt();
		for(int i = 0;i < n;i ++)
			for(int j = 0;j < m;j ++)
			{
				a[i][j] = nextInt();
				if(a[i][j] == 1) cnt ++;
			}
		for(int x = 0;x < n;x ++)
			for(int y = 0;y < m;y ++)
			{
				int cnt1 = 0;
				if(a[x][y] != 1) cnt1 ++;
				for(int i = 0;i < 8;i ++)
				{
					int x1 = x + dx[i],y1 = y + dy[i];
					if(x1 < 0 || x1 > n || y1 < 0 || y1 > m) continue;
					if(a[x1][y1] != 1) cnt1 ++;
				}
				if(cnt1 == 9) a[x][y] = 2;
			}
		
		
//		for(int x = 0;x < n;x ++)
//		{
//			for(int y = 0;y < m;y ++)
//				cout.print(a[x][y] + " ");
//			cout.println();
//		}
//		cout.println();
		
		for(int x = 0;x < n;x ++)
			for(int y = 0;y < m;y ++)
				dfs(x,y);
		
		
		
//		for(int x = 0;x < n;x ++)
//		{
//			for(int y = 0;y < m;y ++)
//				cout.print(a[x][y] + " ");
//			cout.println();
//		}
		
		
		int ans = n*m - cnt;
		cout.print(ans);
		cout.flush();
		
		
		
	}

}

2023/1/13 22:53
加载中...