Java P3663 Why Did the Cow Cross the Road III S

63 篇文章 1 订阅

题目链接
这个很棒,知道了用三位数组存储二维图的方法。
用dfs

import java.util.*;
import java.math.*;
import java.io.*;
public class Main {
	public static int N,K,R,niu,map[][][]=new int[102][102][4],cow[][]=new int[102][202],is[][];
	public static int dx[]={-1,1,0,0};
	public static int dy[]={0,0,-1,1};
    public static void main(String[] args){
    	Scanner sc=new Scanner(System.in);
    	Map<Integer,Integer> m=new HashMap<Integer,Integer>();
    	N=sc.nextInt();
    	K=sc.nextInt();
    	R=sc.nextInt();
    	is=new int[N+1][N+1];
    	for(int i=0;i<R;i++){  //0上 1下,2左,3右
    		int x1,x2,y1,y2;
    		x1=sc.nextInt();
    		y1=sc.nextInt();
    		x2=sc.nextInt();
    		y2=sc.nextInt();
    		if(x1==x2){
    			map[x1][Math.min(y1,y2)][3]=1;
    			map[x1][Math.max(y1,y2)][2]=1;
    		}
    		else{
    			map[Math.min(x1,x2)][y1][1]=1;
    			map[Math.max(x1,x2)][y1][0]=1;
    		}
    	}
    	for(int i=0;i<K;i++){
    		cow[sc.nextInt()][sc.nextInt()]=1;
    	}
    	int ji=0;
    	for(int i=1;i<=N;i++){
    		for(int j=1;j<=N;j++){
    			if(is[i][j]==0){
    				niu=0;
    				dfs(i,j);
    				m.put(ji,niu);
    				ji++;
    			}
    		}
    	}
    	//System.out.println(m);
    	int res=0;
    	for(int i=0;i<m.size();i++){
    		for(int j=i+1;j<m.size();j++)
    			res+=m.get(i)*m.get(j);
    	}
    	System.out.println(res);
    }
    public static void dfs(int x,int y){
    	is[x][y]=1;
    	if(cow[x][y]==1)
    		niu++;
    	for(int i=0;i<4;i++){
    		if(map[x][y][i]==1)
    			continue;
    		int xx=x+dx[i];
    		int yy=y+dy[i];
    		if(xx==0||yy==0||xx>N||yy>N||is[xx][yy]==1)
    			continue;
    		dfs(xx,yy);
    	}
    }
}
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小崔崔谁用的

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值