首页 > 其他分享 >CS106B(2022 winter) hw4

CS106B(2022 winter) hw4

时间:2024-06-05 16:01:21浏览次数:30  
标签:city Set numCities winter unreadyCities roadNetwork 2022 supplyLocations hw4

重生之你在美国当总统

题目描述

We'll say that a country is disaster-ready if every city either already has emergency supplies or is immediately down the highway from a city that has them. Your task is to write a function


bool canBeMadeDisasterReady(const Map<string, Set<string>>& roadNetwork,
                            int numCities,
                            Set<string>& supplyLocations);

takes as input a Map representing the road network for a region and the number of cities you can afford to put supplies in, then returns whether it's possible to make the region disaster-ready without placing supplies in more than numCities cities. If so, the function should then populate the argument supplyLocations with all of the cities where supplies should be stored.

代码


bool canBeMadeDisasterReadyHelper(const Map<string, Set<string>>& roadNetwork,
                                  int numCities,
                                  Set<string>& supplyLocations,Set<string>& unreadyCities){
    if(roadNetwork.size() == 0) return true;

    if(unreadyCities.size() == 0)  return true;
    else if(numCities <= 0) return false;

    //stockpile in it
    string city = unreadyCities.first();
    supplyLocations.add(city);

    Set<string> remainingCities = unreadyCities - city - roadNetwork[city];

    bool result1 = canBeMadeDisasterReadyHelper(roadNetwork,numCities - 1,supplyLocations,remainingCities);

    if(result1) return result1;
    else supplyLocations.remove(city);

    Set<string> neighborCities = roadNetwork[city];
    for(const string &neighbor:neighborCities){

        supplyLocations.add(neighbor);
        Set<string> remainingCities = unreadyCities - neighbor - roadNetwork[neighbor];
        bool result2 = canBeMadeDisasterReadyHelper(roadNetwork,numCities - 1,supplyLocations,remainingCities);
        if(result2) return result2;
        else supplyLocations.remove(neighbor);
    }
    return false;
}


bool canBeMadeDisasterReady(const Map<string, Set<string>>& roadNetwork,
                            int numCities,
                            Set<string>& supplyLocations) {
    /* TODO: Delete the next few lines and implement this function. */
    if(numCities < 0) error("error");
    Set<string> unreadyCities;
    for(const string &city : roadNetwork){
        unreadyCities.add(city);
        Set<string> neighborCities = roadNetwork[city];
        for(const string &s: neighborCities){
            unreadyCities.add(s);
        }
    }

    return canBeMadeDisasterReadyHelper(roadNetwork,numCities,supplyLocations,unreadyCities);
}

后记

自己的思路是用Map<string,int>来存储每个城市的状态,但是一直有bug,看了知乎某个大佬的解答才转换了思路。

标签:city,Set,numCities,winter,unreadyCities,roadNetwork,2022,supplyLocations,hw4
From: https://www.cnblogs.com/EavenWang/p/18233197

相关文章

  • ASP.NET Web应用程序升级最新的MSBuild格式后,Visual Studio 2022中如何调试?
    摘要把ASP.NET的Web应用程序,Project文件从<ProjectToolsVersion="12.0"DefaultTargets="Build"xmlns="http://schemas.microsoft.com/developer/msbuild/2003">改为<ProjectSdk="Microsoft.NET.Sdk.Web">之后,升级成了最新的格式之后,如......
  • ASP.NET Web应用程序升级最新的MSBuild格式后,Visual Studio 2022中如何调试?
    摘要把ASP.NET的Web应用程序,Project文件从<ProjectToolsVersion="12.0"DefaultTargets="Build"xmlns="http://schemas.microsoft.com/developer/msbuild/2003">改为<ProjectSdk="Microsoft.NET.Sdk.Web">之后,升级成了最新的格式之后,如......
  • 【数据分享】中国民政统计年鉴(1949-2022)
    大家好!今天我要向大家介绍一份重要的中国民政统计数据资源——《中国民政统计年鉴》。这份年鉴涵盖了从1949年到2022年中国民政统计全面数据,并提供限时免费下载。(无需分享朋友圈即可获取)数据介绍从1949年到2022年,每年的《中国民政统计年鉴》不仅是数据记录的集合,更是我国社会......
  • P8779 [蓝桥杯 2022 省 A] 推导部分和
    原题链接题解1.集合+搜索2.把数字看成间隔而不是点3.类似于差分约束,这里的建边意味着相对大小,根据传递性可知,如果ab建边,bc建边,那么ac之间的关系也能确定,可以用搜索维护所以unknown代表两个点没有之间或者间接的边相连,可以用集合维护code#include<bits/stdc++.h>#definel......
  • 16位简单ASM题的记录——[HGAME 2022 week1]easyasm
    第一次遇见16位,和纯看汇编的题目,记录一下DIE16位,IDA用32位或者64位都可以打开IDA主要汇编部分seg003:0000;===============SUBROUTINE=======================================seg003:0000seg003:0000;Attributes:noreturnseg003:0000seg003:0000......
  • Visual Studio 2022创建C/C++项目
    没想到还有能用到C/C++的时候……刚好忘记怎么用VisualStudio了,写个博客记录一下 参考——https://blog.csdn.net/Long_xu/article/details/130599633https://learn.microsoft.com/zh-cn/visualstudio/extensibility/vsix/get-started/get-tools?view=vs-2022版本:VisualS......
  • SQLServer2022创建表及字段增加备注
    SQLServer2022创建表及字段增加备注,导入Oracle12c示例数据库HRschema下的表及数据。在SQLServer中,可以使用系统视图sys.extended_properties来查看表或字段的描述官方文档地址https://learn.microsoft.com/en-us/sql/ssms/visual-db-tools/column-properties-visual-dat......
  • 微服务实践之使用 Visual Studio 2022 调试Dapr 应用程序
    安装配置相关软件安装PowerShell7/Coredotnettoolinstall--globalPowerShell安装VisualStudio扩展MicrosoftChildProcessDebuggingPowerTool2022安装插件后启动VisualStudio,可以在Debug->OtherDebuggingTargets中找到ChildProcessDebuggingSet......
  • Windows Server 2022 中 wbadmin 工具
    WindowsServer2022中wbadmin工具的初级应用大纲:1.理解wbadmin工具介绍wbadmin工具及其作用。解释wbadmin在WindowsServer2022中的重要性和作用。2.基本备份操作学习如何使用wbadmin工具执行基本备份操作。包括备份到本地磁盘或网络共享的示例。3.......
  • Windows Server 2022上使用DFS文件服务器
    WindowsServer2022上使用DFS(分布式文件系统)作为文件服务器的初级应用大纲:课程内容介绍DFS什么是DFS?DFS的优势和应用场景部署WindowsServer2022WindowsServer2022的安装和配置安装DFS角色在WindowsServer2022上安装DFS角色创建DFS命名空间创建DFS......