1039. Phone Home

染色问题初探。
问题就是模拟蜂窝染色。
方法一,对每个点,扫描周围相邻的每一个点,从1(颜色)开始,有重复则递增,会找到染本点的最小数字。

                所有点中最大的数即为总共需要的颜色数。O(n^2)的·复杂度。

              //  居然AC了...说明测试数据的局限性!!!.

              // 这个方法后来举个反例证明是错误的,如果按照度数由大到小的顺序搜索呢?也不知道对不对。

后来发现好像对着呢,,,把当初的疑惑注释掉....一下就是举得例子,发现当时算错了,也不能推翻我的算法


#include<stdio.h>
#include<cmath>
#include<cstring>
const double eps = 1e-8;


double x[12], y[12];
int map[12][12];
int n, tot;
int  color[12];
inline double dis(double x0, double y0, double x1, double y1){
       return sqrt( (x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0));
}
void dfs(int k){
    if(!color[k]){
         int i , j, tc = 1;
         bool flg = 1;
         while(flg){
             flg = 0;
             for(i = 0; i < n; i++) if(map[k][i]){
                 if(tc == color[i]){
                     tc++; flg = 1;
                     break;
                 } 
             }
         }

         color[k] = tc;
  }
}
int main(){
   //freopen("in.txt", "r", stdin);
    int cas = 0;
    while(scanf("%d" , &n)!=EOF && n){
           cas++;
           int i, j;
           for(i = 0; i < n; i++ )scanf("%lf%lf", x + i, y + i);
           memset(map, 0, sizeof(map));
           memset(color, 0, sizeof(color));
           
           for(i = 0; i < n; i++){
               for(j = 0; j < n; j++)if(i != j){
                     if( dis(x[i], y[i], x[j], y[j]) <= 20 + eps)map[i][j] = 1;
               }
           }
          tot = 0;
          for(i = 0; i < n; i++){
              dfs(i);
              if(color[i] > tot)tot = color[i];
          }
          
      printf("The towers in case %d can be covered in %d frequencies.\n", cas, tot);
    }
}



正规解法是深搜,我加了注释。是lh 写的,有点难懂

#include <stdio.h>
#include <math.h>

const double eps = 1e-6;

typedef struct
{
	double x, y;
}POINT;

POINT tower[12];
int graph[12][12];
int color[12];
int n;

double dist(POINT a, POINT b)
{
	return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}

int search(int dep, int num)//num种颜色可以成功么?
{
	int i, j;
	if (dep == n) return 1;
	for (i=0; i<num; i++)//dep 染 i 可以么?
	{   //当前只染到dep
		for (j=0; j<dep; j++) if (graph[dep][j] && color[j] == i) break;
		if (j < dep) continue;
		//以上2句:若果当前dep不能染 i ,继续i++。如果可以,则j>=dep,执行以下
		color[dep] = i;
		if (search(dep + 1, num)) return 1;
	}
	return 0;
}

int main()
{
	int cn, i, j;
	cn = 1;
	while (scanf("%d", &n), n > 0)
	{
		for (i=0; i<n; i++) scanf("%lf%lf", &tower[i].x, &tower[i].y);
		for (i=0; i<n; i++)
			for (j=0; j<n; j++)
				if (i != j && dist(tower[i], tower[j]) <= 20 + eps) graph[i][j] = 1; else graph[i][j] = 0;
		for (i=1; i<=5; i++)
		{
			color[0] = 0;
			if (search(1, i)) break;
		}
		printf("The towers in case %d can be covered in %d frequencies.\n", cn, i);
		cn ++;
	}
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
class Contact { protected String name; protected String phone; protected String remark; public Contact(String name, String phone, String remark) { this.name = name; this.phone = phone; this.remark = remark; } public void display() { System.out.println(toString()); } @Override public String toString() { return "姓名:" + name + ",电话:" + phone + ",备注:" + remark; } } class Family extends Contact { private String address; private String homePhone; public Family(String name, String phone, String remark, String address, String homePhone) { super(name, phone, remark); this.address = address; this.homePhone = homePhone; } @Override public void display() { System.out.println(toString()); } @Override public String toString() { return super.toString() + ",地址:" + address + ",固定电话:" + homePhone; } } class Friend extends Contact { private String category; public Friend(String name, String phone, String remark, String category) { super(name, phone, remark); this.category = category; } @Override public void display() { System.out.println(toString()); } @Override public String toString() { return super.toString() + ",类别:" + category; } } class WorkContact extends Contact { private String company; private String department; private String position; public WorkContact(String name, String phone, String remark, String company, String department, String position) { super(name, phone, remark); this.company = company; this.department = department; this.position = position; } @Override public void display() { System.out.println(toString()); } @Override public String toString() { return super.toString() + ",公司:" + company + ",部门:" + department + ",职务:" + position; } }此程序的UML图怎么画
05-26

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值