当前位置:网站首页>P2102 地砖铺设(dfs&贪心)
P2102 地砖铺设(dfs&贪心)
2022-07-06 04:11:00 【Harris-H】
P2102 地砖铺设(dfs&贪心)
枚举每个点作为正方形左上角进行扩展,只需对左边界和上边界进行判断即可,然后正方形填充。因为要求最小,所以每次就特判右上角的点是否能更小,如果不行就break,否则就继续扩展。
参考代码
#include<bits/stdc++.h>
using namespace std;
char a[502][502];
int n,m,x,y;
bool same(char k,int x,int y) //判断(x,y)是否可以放入k
{
if (x>n||y>m) return false; //判断有没有越界
if (a[x-1][y]==k&&a[x-1][y]) return false;
if (a[x+1][y]==k&&a[x+1][y]) return false;
if (a[x][y-1]==k&&a[x][y-1]) return false;
if (a[x][y+1]==k&&a[x][y+1]) return false; //判断有没有与四周相同的
return true;
}
int pd(char k,int x1,int y1) //x1,y1表示扩展正方形的左上角坐标
{
x=x1;
y=y1; //x,y表示无法扩展到的行标和列标
int flag=0; //flag表示扩展是否成功
for (int i=1;i<=n;i++)
if (!a[x][y1]&&!a[x1][y]&&same(k,x,y1)&&same(k,x1,y))
//判断当前位置有没有被放过并且有没有与周围相同
{
char min1=0;
for (char ch='A';ch<k;ch++) //判断右侧位置能否更小
if (same(ch,x1,y))
{
min1=ch;
break;
}
if (min1) break; //如果发现右侧可以放更小的,就退出扩展
flag=1;
x++;y++; //往下扩展
}
else break;
for (int i=x1;i<x;i++) //因为x,y表示无法扩展,所以取小于号
for (int j=y1;j<y;j++) a[i][j]=k; //赋值,扩展
return flag;
}
int main()
{
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
if (!a[i][j])
for (char k='A';k<='Z';k++) //从小到大枚举当前位置可以放的最小值
{
int x=pd(k,i,j);
if (x) break; //如果成功放入,说明当前位置已经放入了最优值,退出
}
for (int i=1;i<=n;i++)
{
for (int j=1;j<=m;j++)
printf("%c",a[i][j]);
printf("\n");
} //愉快输出
return 0;
}
边栏推荐
- QML和QWidget混合开发(初探)
- Global and Chinese market of plasma separator 2022-2028: Research Report on technology, participants, trends, market size and share
- Leetcode32 longest valid bracket (dynamic programming difficult problem)
- Global and Chinese markets for patent hole oval devices 2022-2028: Research Report on technology, participants, trends, market size and share
- Web components series (VII) -- life cycle of custom components
- What is the difference between gateway address and IP address in tcp/ip protocol?
- The global and Chinese market of negative pressure wound therapy unit (npwtu) 2022-2028: Research Report on technology, participants, trends, market size and share
- Query the number and size of records in each table in MySQL database
- AcWing 243. A simple integer problem 2 (tree array interval modification interval query)
- [PSO] Based on PSO particle swarm optimization, matlab simulation of the calculation of the lowest transportation cost of goods at material points, including transportation costs, agent conversion cos
猜你喜欢

食品行业仓储条码管理系统解决方案

综合能力测评系统

Detailed explanation of serialization and deserialization

Figure application details

math_ Derivative function derivation of limit & differential & derivative & derivative / logarithmic function (derivative definition limit method) / derivative formula derivation of exponential functi

【按鍵消抖】基於FPGA的按鍵消抖模塊開發

About some basic DP -- those things about coins (the basic introduction of DP)

User datagram protocol UDP
![[introduction to Django] 11 web page associated MySQL single field table (add, modify, delete)](/img/8a/068faf3e8de642c9e3c4118e6084aa.jpg)
[introduction to Django] 11 web page associated MySQL single field table (add, modify, delete)

【按键消抖】基于FPGA的按键消抖模块开发
随机推荐
hashlimit速率控制
About some basic DP -- those things about coins (the basic introduction of DP)
Basic use of MySQL (it is recommended to read and recite the content)
[adjustable delay network] development of FPGA based adjustable delay network system Verilog
使用JS完成一个LRU缓存
10 exemples les plus courants de gestion du trafic istio, que savez - vous?
AcWing 243. A simple integer problem 2 (tree array interval modification interval query)
Determine which week of the month the day is
Global and Chinese market of rubber wheel wedges 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese markets for endoscopic drying storage cabinets 2022-2028: Research Report on technology, participants, trends, market size and share
What is the difference between gateway address and IP address in tcp/ip protocol?
math_ Derivative function derivation of limit & differential & derivative & derivative / logarithmic function (derivative definition limit method) / derivative formula derivation of exponential functi
The global and Chinese market of negative pressure wound therapy unit (npwtu) 2022-2028: Research Report on technology, participants, trends, market size and share
Esp32 (based on Arduino) connects the mqtt server of emqx to upload information and command control
Execution order of scripts bound to game objects
题解:《单词覆盖还原》、《最长连号》、《小玉买文具》、《小玉家的电费》
When debugging after pycharm remote server is connected, trying to add breakpoint to file that does not exist: /data appears_ sda/d:/segmentation
综合能力测评系统
Deep learning framework installation (tensorflow & pytorch & paddlepaddle)
深入浅出node模板解析错误escape is not a function