当前位置:网站首页>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;
}
边栏推荐
- DM8 backup set deletion
- Explain in simple terms node template parsing error escape is not a function
- Maxay paper latex template description
- Codeforces Round #770 (Div. 2) B. Fortune Telling
- Database, relational database and NoSQL non relational database
- Ks003 mall system based on JSP and Servlet
- math_ Derivative function derivation of limit & differential & derivative & derivative / logarithmic function (derivative definition limit method) / derivative formula derivation of exponential functi
- [001] [stm32] how to download STM32 original factory data
- Benefits of automated testing
- [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
猜你喜欢
AcWing 243. A simple integer problem 2 (tree array interval modification interval query)
Record the pit of NETCORE's memory surge
Stack and queue
Class A, B, C networks and subnet masks in IPv4
Simple blog system
Développement d'un module d'élimination des bavardages à clé basé sur la FPGA
Mlapi series - 04 - network variables and network serialization [network synchronization]
【按键消抖】基于FPGA的按键消抖模块开发
math_ Derivative function derivation of limit & differential & derivative & derivative / logarithmic function (derivative definition limit method) / derivative formula derivation of exponential functi
Figure application details
随机推荐
Recommendation system (IX) PNN model (product based neural networks)
Practical development of member management applet 06 introduction to life cycle function and user-defined method
What is the difference between gateway address and IP address in tcp/ip protocol?
Viewing and verifying backup sets using dmrman
STC8H开发(十二): I2C驱动AT24C08,AT24C32系列EEPROM存储
Detailed explanation of serialization and deserialization
ESP32(基于Arduino)连接EMQX的Mqtt服务器上传信息与命令控制
Global and Chinese market of aircraft anti icing and rain protection systems 2022-2028: Research Report on technology, participants, trends, market size and share
Ks008 SSM based press release system
【FPGA教程案例12】基于vivado核的复数乘法器设计与实现
Thread sleep, thread sleep application scenarios
Hashcode and equals
Stc8h development (XII): I2C drive AT24C08, at24c32 series EEPROM storage
自动化测试的好处
R note prophet
Record an excel xxE vulnerability
2/10 parallel search set +bfs+dfs+ shortest path +spfa queue optimization
DM8 backup set deletion
Lambda expression learning
《2022年中国银行业RPA供应商实力矩阵分析》研究报告正式启动